序言:加入Android开发并不久,但是自从踏入这条路,我就蛮喜欢Android开发,一路上虽然遇到很多苦难,但是我相信只要坚持就一定能够学好。也希望大家能够一起进步!

今天为大家带来一篇控件的自定义属性的文章

很多初级的android开发工作者都听说过自定义控件 自定义属性,但是实际操作起来似乎很麻烦,遇到很多问题,我也一样在这过程中走了很多弯路,希望我下面的总结能对读者有所帮助。

步骤:

1.将已经编写好的布局文件,抽取到一个类中去做管理,下次还需要使用此布局结构的时候,直接使用组合控件对应的对象.

2.将组合控件的布局,抽取到单独的一个xml中

3.通过一个单独的类,去加载此段布局文件.

代码:

在res/values这个资源下面找到attrs这个xml文件,如果没有自己创建

1,SettingItemView是你自定义的控件的类名,这里需要完整路径
2,name后面是属性的名字 format是属性的类型;

">        <attr name="destitle" format="string"/>
        <attr name="desoff" format="string"/>
        <attr name="deson" format="string"/>
    </declare-styleable>

=====》ItemView 的layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_below="@+id/tv_setting_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_title"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="自动更新设置"
            android:textSize="18sp"
            android:textColor="#000"
            />
        <TextView
            android:id="@+id/tv_des"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_title"
            android:layout_marginLeft="10dp"
            android:text="自动更新已经关闭"
            android:textSize="18sp"
            android:textColor="#000"
            />
        <CheckBox
            android:id="@+id/cb_box"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_below="@id/tv_des"
            android:background="#000"/>
    </RelativeLayout>
</LinearLayout>


//这里是继承RelativeLayout
  public class SettingItemView extends RelativeLayout{
  //NAMESPACE是属性的命名空间
 private static final String NAMESPACE = "http://schemas.android.com/apk/res-auto";
    CheckBox  cb_box ;
    TextView  tv_des ;
    TextView  tv_title ;
    String mDestitle ;
    String mDesoff ;
    String mDeson ;
    //这里是三个构造方法,我们让第一个构造方法调用第二个构造方法,第二个构造方法调用第三个构造方法,最后所有的代码都写在第三个构造方法里面 context上下文  AttributeSet 属性集合 defStyleAttr样式。
    public SettingItemView(Context context) {
        this(context,null);
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //将设置界面的一个条目转化为一个对象,我们要写一个Layoutitem作为我们组合自定义控件的样式,item怎么布局,我们的控件以后就怎么显示
        View itemView = View.inflate(context, R.layout.settting_item_view,null);
        this.addView(itemView);//把刚布局的item加进来作为界面,下面可以通过findViewById找到这个几个组合的基本控件。

       tv_title = (TextView)itemView.findViewById(R.id.tv_title);
       tv_des = (TextView)itemView.findViewById(R.id.tv_des);
       cb_box = (CheckBox)itemView.findViewById(R.id.cb_box);

        //获取自定义及原生的属性
        initAttrs(attrs);
    //获取布局文件中的字符串,赋值给自定义组合控件的标题
        tv_title.setText(mDestitle);
        Log.i("=======",mDestitle);
    }

=======》现在我们在主界面使用这个控件

<test.huawei.com.myapplication.view.SettingItemView
android:id="@+id/siv_update"
android:layout_below="@id/tv_setting_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
mobilesafe:destitle="自动更新设置"
mobilesafe:desoff="自动更新已关闭"
mobilesafe:deson="自动更新已开启">
</test.huawei.com.myapplication.view.SettingItemView>


在自定义控件类里卖弄我们可以这样做
private void initAttrs(AttributeSet attributeset) {
int arrtsCount = attributeset.getAttributeCount();//拿到该控件的属性的数目,自定义的也包括
//获取属性名称以及属性值
// for (int i=0;i<arrtsCount;i++){
// String name = attributeset.getAttributeName(i);//属性的名字
// String value = attributeset.getAttributeValue(i);//属性的值
// }
mDestitle =attributeset.getAttributeValue(NAMESPACE,"destitle");
mDesoff = attributeset.getAttributeValue(NAMESPACE,"desoff");
mDeson = attributeset.getAttributeValue(NAMESPACE,"deson");
//通过以上的代码我们就可以将刚在在xml文件中的属性值全部拿到。
}

//获取自定义及原生的属性,完成这个方法就可以赋值了
    initAttrs(attrs);
 //获取布局文件中的字符串,赋值给自定义组合控件的标题
    tv_title.setText(mDestitle);
    Log.i("=======",mDestitle);

以上我写的代码可能有人越看越复杂,其实理解了就会很简单,如果文中有什么疑惑 可以直接给我留言