自定义控件对属性的操作

本文介绍了一个自定义的CombineView组件实现过程,该组件继承自FrameLayout并集成了TextView和CheckBox的功能。通过XML属性设置文本内容和CheckBox的状态,并在构造函数中完成了初始化。
public class ComBineView extends FrameLayout {
    private String text;
    private boolean isck;

    public ComBineView(@NonNull Context context) {
        super(context);
        init();
    }

    public ComBineView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        text = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "text");
        isck = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "ck", true);
        init();
    }

    public ComBineView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        //把布局挂载到CombineView上...viewGroup root表示是否有挂载的父控件
        View view = View.inflate(getContext(), R.layout.combineview_layout, this);
        TextView textView= view.findViewById(R.id.text);
        CheckBox ck= view.findViewById(R.id.ck);

        textView.setText(text);
         ck.setChecked(isck);

    }

}
attrs
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ComBineView">
        <attr name="text" format="string"></attr>
        <attr name="ck" format="boolean"></attr>
    </declare-styleable>
</resources>

ComBineView 中提供对外的传值得方法可以对自定义控件进行改值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值