自定义布局-自定义RelativeLayout

package net.feelingtech.example_work.custom.productitemlayout;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.feelingtech.example_work.R;

/**
 * Created by ZLM on 2018/5/17.
 * Describe 自定义RelativeLayout
 */

public class ProductItemLayout extends RelativeLayout {

    private ImageView      mItem_product_icon;
    private TextView       mItem_product_title;
    private TextView       mItem_product_new;
    private TextView       mItem_product_percent;
    private RelativeLayout mItem_product_new_rl;
    private RelativeLayout mItem_product_percent_rl;

    public ProductItemLayout(Context context) {
        this(context, null);
    }

    public ProductItemLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProductItemLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.product_item_layout, this);
        initView(view);
        initAttrs(context, attrs);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(R.styleable.ProductItemLayout);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            initAttr(typedArray.getIndex(i), typedArray);
        }
        typedArray.recycle();
    }

    private void initAttr(int attr, TypedArray typedArray) {
        if (attr == R.styleable.ProductItemLayout_item_product_icon) {
            //设置图标
            setProductIcon(typedArray.getDrawable(attr));
        }
        if (attr == R.styleable.ProductItemLayout_item_product_title) {
            setProductTitle(typedArray.getText(attr));
        }
        if (attr == R.styleable.ProductItemLayout_item_product_new) {
            setProductNew(typedArray.getText(attr));
        }
        if (attr == R.styleable.ProductItemLayout_item_product_percent) {
            setProductPercent(typedArray.getText(attr));
        }
        if (attr == R.styleable.ProductItemLayout_item_product_percent_isshow) {
            boolean isShow = typedArray.getBoolean(attr, false);
            setProductPercentIsShow(isShow);
        }
        if (attr == R.styleable.ProductItemLayout_item_product_new_isshow) {
            boolean isShow = typedArray.getBoolean(attr, false);
            setProductNewIsShow(isShow);
        }
    }

    public void setProductNewIsShow(boolean isShow) {
        if (isShow) {
            mItem_product_new_rl.setVisibility(VISIBLE);
        } else {
            mItem_product_new_rl.setVisibility(GONE);
        }
    }

    public void setProductPercentIsShow(boolean isShow) {
        if (isShow) {
            mItem_product_percent_rl.setVisibility(VISIBLE);
        } else {
            mItem_product_percent_rl.setVisibility(GONE);
        }
    }

    public void setProductPercent(CharSequence text) {
        mItem_product_percent.setText(text);
    }

    public void setProductNew(CharSequence text) {
        mItem_product_new.setText(text);
    }

    private void initView(View view) {
        //图标
        mItem_product_icon = view.findViewById(R.id.item_product_icon);
        //标题
        mItem_product_title = view.findViewById(R.id.item_product_title);
        //是否有新的内容
        mItem_product_new = view.findViewById(R.id.item_product_new);
        //百分率
        mItem_product_percent = view.findViewById(R.id.item_product_percent);
        //是否有新的内容是否显示提示
        mItem_product_new_rl = view.findViewById(R.id.item_product_new_rl);
        //百分率是否显示
        mItem_product_percent_rl = view.findViewById(R.id.item_product_percent_rl);

    }

    public void setProductIcon(Drawable productIcon) {
        mItem_product_icon.setImageDrawable(productIcon);
    }

    public void setProductTitle(CharSequence productTitle) {
        mItem_product_title.setText(productTitle);
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:gravity="center_vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/item_product_icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:src="@drawable/finance_icon01"/>

    <TextView
        android:id="@+id/item_product_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="益民理财"
        android:layout_below="@id/item_product_icon"
        android:textColor="@color/black"
        android:textSize="12sp"/>

    <RelativeLayout
        android:id="@+id/item_product_new_rl"
        android:layout_width="wrap_content"
        android:layout_height="12dp"
        android:layout_alignBaseline="@id/item_product_title"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@id/item_product_title"
        android:background="#EE6147"
        android:gravity="center">

        <TextView
            android:id="@+id/item_product_new"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="2dp"
            android:paddingRight="2dp"
            android:text="NEW"
            android:textColor="@color/white"
            android:textSize="10dp"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/item_product_percent_rl"
        android:layout_width="wrap_content"
        android:layout_height="18dp"
        android:layout_alignTop="@id/item_product_icon"
        android:layout_marginLeft="-5dp"
        android:layout_marginTop="-10dp"
        android:layout_toRightOf="@id/item_product_icon"
        android:background="#EE6147"
        android:gravity="center_vertical"
        android:padding="2dp">

        <TextView
            android:id="@+id/item_product_percent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="2dp"
            android:paddingRight="2dp"
            android:text="6%-7%"
            android:textColor="@color/white"
            android:textSize="10dp"/>

    </RelativeLayout>


</RelativeLayout>

attr文件:


<declare-styleable name="ProductItemLayout">

        <attr name="item_product_icon" format="reference"/>
        <attr name="item_product_title" format="reference|string"/>
        <attr name="item_product_new" format="reference|string"/>
        <attr name="item_product_percent" format="reference|string"/>
        <attr name="item_product_percent_isshow" format="reference|boolean"/>
        <attr name="item_product_new_isshow" format="reference|boolean"/>

    </declare-styleable>

===========================================================

使用:

<LinearLayout
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <net.feelingtech.example_work.custom.productitemlayout.ProductItemLayout
            android:id="@+id/pil_jr"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </net.feelingtech.example_work.custom.productitemlayout.ProductItemLayout>

        <net.feelingtech.example_work.custom.productitemlayout.ProductItemLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </net.feelingtech.example_work.custom.productitemlayout.ProductItemLayout>

        <net.feelingtech.example_work.custom.productitemlayout.ProductItemLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </net.feelingtech.example_work.custom.productitemlayout.ProductItemLayout>

    </LinearLayout>
mPilJr.setProductIcon(getResources().getDrawable(R.drawable.tab_icon_home));
        mPilJr.setProductTitle("金融");
        mPilJr.setProductNewIsShow(true);
        mPilJr.setProductNew("NNN");
        mPilJr.setProductPercentIsShow(true);
        mPilJr.setProductPercent("6%-9%");




猜你喜欢

转载自blog.csdn.net/aminy123/article/details/80399391