效果呈现:
记录学习过程:
1.新建一个类继承布局LinearLayout,实现两个参数的构造方法。
/**
* 继承布局,自定义ItemView
*/
public class ItemView extends LinearLayout {
@BindView(R.id.iv_itemview_icon)
ImageView iv_itemview_icon;
@BindView(R.id.tv_itemview_text)
TextView tv_itemview_text;
@BindView(R.id.iv_itemview_right)
ImageView iv_itemview_right;
@BindView(R.id.v_itemview_line)
View v_itemview_line;
@BindView(R.id.ll_itemview)
LinearLayout ll_itemview;
public ItemView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.view_itemview_layout,this);
ButterKnife.bind(this);
//获取属性的值
TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.ItemView);
int setIcon=typedArray.getResourceId(R.styleable.ItemView_setIcon,R.mipmap.ic_my_d);
String setText=typedArray.getString(R.styleable.ItemView_setText);
Boolean isShowRightIcon=typedArray.getBoolean(R.styleable.ItemView_isShowRightIcon,true);
Boolean isShowLine=typedArray.getBoolean(R.styleable.ItemView_isShowLine,true);
//通过绑定属性设置自定义的View
setView(setIcon,setText,isShowRightIcon,isShowLine);
//回收
typedArray.recycle();