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"?>                                        

attr文件:


                                                    

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

使用:

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




更多相关文章

  1. Android启动页用户相关政策弹框的实现
  2. Android中常见的Bug
  3. Android(安卓)- 分享内容 - 给其他APP发送内容
  4. 设计应用Logo Android(安卓)- Asset Studio Integration
  5. android 系统权限大全的简介与内容
  6. android:windowSoftInputMode="adjustResize" >
  7. android 软件参数设置的保存和读取 附源码
  8. 写给程序员和UI--Android的切图标准
  9. Android(安卓)为什么TextView文本有内容却显示为空?

随机推荐

  1. CSS学习(一)
  2. Linux管理员和普通用户
  3. css元素选择器
  4. 一、简单学习Ajax的GET & POST请求
  5. 干货丨手把手教你如何加载和操作DolphinD
  6. Terraform踩坑记之:Azure Provider配置
  7. 模拟器如何换IP
  8. virt-viewer编译:syntax error near unexp
  9. 马斯克的星链计划对互联网有哪些影响?
  10. Linux系统实战——批量无人值守安装操作