Android通过继承布局自定义ItemView

本文介绍了如何在Android中通过继承LinearLayout自定义ItemView,详细步骤包括新建继承类、设计布局、定义属性、获取属性值以及设置点击事件。通过这个教程,你可以学会如何为你的应用创建独特的列表项视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

效果呈现:

 

记录学习过程:

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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值