自定义控件--自定义控件的属性

本文基于上篇博客,深入探讨如何在布局文件中为自定义组合控件自定义属性,优化控件功能,并详细介绍了XML声明、属性获取及Activity实现等关键步骤。此外,文章还阐述了两种获取控件属性的方法,并通过实例代码演示了具体操作。

在上一篇博客自定义组合控件的基础上,给自定义的组合控件自定义几个属性,优化上一篇博客的组合控件

步骤一:在布局文件中给某个控件自定义属性

例如这里给CustomWidgetAttrView控件自定义yzjdev:widget_title,yzjdev:desc_on,yzjdev:desc_off三个属性,同时需指定命名空间,对于命名空间Eclipse和Android Studio

有所不同,在Android studio中,因为是用Gradle构建的所有:xmlns:yinattr="http://schemas.android.com/apk/res-auto"
    在Eclipse中,因为是用ant构建的所以:xmlns:yinattr="http://schemas.android.com/apk/res/包名"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yzjdev="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:textSize="30sp"
        android:background="#A52A2A"
        android:text="@string/tv_setting_text"/>
    <!--自定义了widget_title、desc_on、desc_off属性-->
    <com.mycompany.mysimple.customwidgetattr.CustomWidgetAttrView
        android:id="@+id/custom_widget"
        yzjdev:widget_title="@string/custom_attr_title"
        yzjdev:desc_on="@string/desc_on_text"
        yzjdev:desc_off="@string/desc_off_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

二:在values目录下新建一个attrs.xml文件

(参考E:\android-sdk-windows\platforms\android-17\data\res\values\attrs.xml中的    TextView),在该xml文件中声明你自定义的属性名和属性值

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--自定义属性-->
    <declare-styleable name="TextView">
        <attr name="widget_title" format="string" />
        <attr name="desc_on" format="string" />
        <attr name="desc_off" format="string" />
    </declare-styleable>
</resources>

三:获取控件属性的两类方式

public class CustomWidgetAttrView extends RelativeLayout {
    private String widget_title,desc_on,desc_off;
    private CheckBox ck_checkbox;
    private TextView tv_isUpdate_widget,tv_isUpdate_state_widget;
    public CustomWidgetAttrView(Context context) {
        super(context);
        initView(context);
    }

    /**
     * 实例化(加载)布局文件中的CustomWidgetAttrView时,执行该构造器
     */
    public CustomWidgetAttrView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        if(attrs == null){
            System.out.println("不是两个参数的构造函数");
        }else{
            System.out.println("是两个参数的构造函数");
        }
        //当你在布局文件中的文本内容是通过@string/...的方式引用的strings.xml文件中的值时,同个下面这个方法无法获取到strings.xml中的值,获取到的是R文件下的id的值
        widget_title = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","widget_title");
        //通过这个方式获取对应的值
        widget_title = getResources().getString(R.string.custom_attr_title);
        tv_isUpdate_widget.setText(widget_title);
        desc_on = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","desc_on");
        desc_off = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","desc_off");
        desc_on = getResources().getString(R.string.desc_on_text);
        desc_off = getResources().getString(R.string.desc_off_text);
        System.out.println("属性结果:widget_title:"+widget_title+",desc_on:"+desc_on+",desc_off:"+desc_off);
        setChecked(false);
    }

    public CustomWidgetAttrView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        if(attrs == null){
            System.out.println("不是三个参数的构造函数");
        }else{
            System.out.println("是三个参数的构造函数");
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomWidgetAttrView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
        if(attrs == null){
            System.out.println("不是四个参数的构造函数");
        }else{
            System.out.println("是四个参数的构造函数");
        }
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.update_item_notext, CustomWidgetAttrView.this);
        ck_checkbox = (CheckBox) this.findViewById(R.id.ck_checkbox_widget);
        tv_isUpdate_widget = (TextView) this.findViewById(R.id.tv_isUpdate_widget);
        tv_isUpdate_state_widget = (TextView) this.findViewById(R.id.tv_isUpdate_state_widget);
    }
    public boolean isChecked(){
        return  ck_checkbox.isChecked();
    }
    public void setChecked(Boolean boo){
        if(boo){
            tv_isUpdate_state_widget.setText(desc_on);
        }else{
            tv_isUpdate_state_widget.setText(desc_off);
        }
        ck_checkbox.setChecked(boo);
    }
}

四:Activity实现

public class CustomWidgetAttrActivity extends FragmentActivity implements View.OnClickListener{

    private CustomWidgetAttrView custom_widget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customwidgetattr);
        custom_widget = (CustomWidgetAttrView) findViewById(R.id.custom_widget);
        custom_widget.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.custom_widget:
                if(custom_widget.isChecked()){
                    custom_widget.setChecked(false);
                }else{
                    custom_widget.setChecked(true);
                }
                break;
            default:
                break;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值