自定义布局-自定义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);
    }
}

布局:




    

    

    

        

    

    

        

    




自定义布局-自定义RelativeLayout_第1张图片

attr文件:

自定义布局-自定义RelativeLayout_第2张图片



        
        
        
        
        
        

    

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

使用:



        

        

        

        

        

        

    
mPilJr.setProductIcon(getResources().getDrawable(R.drawable.tab_icon_home));
        mPilJr.setProductTitle("金融");
        mPilJr.setProductNewIsShow(true);
        mPilJr.setProductNew("NNN");
        mPilJr.setProductPercentIsShow(true);
        mPilJr.setProductPercent("6%-9%");
自定义布局-自定义RelativeLayout_第3张图片



你可能感兴趣的:(Android)