Android自定义控件——自定义属性

来源:互联网 发布:阿里云服务器 ftp 编辑:程序博客网 时间:2024/05/19 02:21

      转载请注明出处:http://blog.csdn.net/allen315410/article/details/39343401

       我们在自定义android组件的时候,除了用Java构建出组件的样子外,有时候还需要去申明一些“属性”提供给项目使用,那么什么是组件的属性呢?

例如在清单文件中,创建一个TextView的时候,这是需要制定TextView的android:layout_width="wrap_content" android:layout_height="wrap_content"等等这些都是组件的属性,TextView是android系统为我们提供好的组件,它的属性亦是android系统为我们提供了。详情查看android的源码,我这里举例android2.3的源码,路径是
/frameworks/base/core/res/res/values/attrs.xml,这个attrs.xml定义了所有android系统组件的属性。

当我们自定义组件时,除了可以使用android系统为我们提供好的属性之外,还可以自定义属性。自定义属性主要步骤如下:
一、在attrs.xml文件中声明属性,如:
[html] view plaincopyprint?
  1. <declare-styleable name="MyToggleBtn">            // 声名属性集的名称,即这些属性是属于哪个控件的。  
  2. <attr name="current_state" format="boolean"/>   // 声名属性 current_state 格式为 boolean 类型  
  3. <attr name="slide_button" format="reference"/>   // 声名属性 slide_button格式为 reference 类型  
  4. </declare-styleable>   
所有的format类型
reference    引用
color           颜色
boolean      布尔值
dimension   尺寸值
float           浮点值
integer       整型值
string         字符串
enum         枚举值

二、在布局文件中使用:在使用之前必须声名命名空间,xmlns:example="http://schemas.android.com/apk/res/com.example.mytogglebtn"
说明:xmlns      是XML name space 的缩写; 
          example   可为任意写符       
          http://schemas.android.com/apk/res/    此为android固定格式;      

          com.example.mytogglebtn    此应用的包名,如manifest配置文件中一致。

布局文件:

[html] view plaincopyprint?
  1. <com.example.mytogglebtn.MyToggleButton  
  2.     xmlns:example="http://schemas.android.com/apk/res/com.example.mytogglebtn"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"   
  5.     example:slide_button="@drawable/slide_button" />  

三、在代码中对属性进行解析,代码如下:

[java] view plaincopyprint?
  1. TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyToggleBtn);// 由attrs 获得 TypeArray  

以上是创建自定义属性的大致步骤。下面,我将要创建一个自定义控件的Demo,来学习学习自定义属性的相关知识点。

首先,需要创建一个自定义控件出来,并且继承View。在工程的res/values文件夹下创建attrs.xml文件:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <!-- 声明属性级的名称 -->  
  5.     <declare-styleable name="MyView">  
  6.   
  7.         <!-- 声明一个属性,整型 -->  
  8.         <attr name="test_id" format="integer" />  
  9.         <!-- 声明一个属性,字符串 -->  
  10.         <attr name="test_msg" format="string" />  
  11.         <!-- 声明一个属性,引用,引用资源id -->  
  12.         <attr name="test_bitmap" format="reference" />  
  13.     </declare-styleable>  
  14.   
  15. </resources>  
然后在布局文件中,引用这个自定义控件MyView

[html] view plaincopyprint?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     xmlns:example="http://schemas.android.com/apk/res/com.example.myattrs"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.   
  7.     <com.example.myattrs.MyView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         example:test_bitmap="@drawable/ic_launcher"  
  12.         example:test_msg="@string/app_name" />  
  13.   
  14. </RelativeLayout>  

由于创建出来的自定义组件MyView是继承于View的,所以必须得复写View的构造方法,View中有三个构造方法,先来看看复写带一个参数的构造方法:

[java] view plaincopyprint?
  1. package com.example.myattrs;  
  2.   
  3. import android.content.Context;  
  4. import android.view.View;  
  5.   
  6. public class MyView extends View {  
  7.   
  8.     public MyView(Context context) {  
  9.         super(context);  
  10.         // TODO Auto-generated constructor stub  
  11.     }  
  12. }  
运行一下工程,那么工程立即崩溃了,报错也很清晰明了:

09-17 06:52:24.389: E/AndroidRuntime(1563): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

表示没有找到某个带两个参数的构造方法,于是,知道自定义属性必须得复写父类的另外一个构造方法,修改如下:

[java] view plaincopyprint?
  1. package com.example.myattrs;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.View;  
  6.   
  7. public class MyView extends View {  
  8.   
  9.     public MyView(Context context, AttributeSet attrs) {  
  10.         super(context, attrs);  
  11.   
  12.         int count = attrs.getAttributeCount();  
  13.         for (int index = 0; index < count; index++) {  
  14.             String attributeName = attrs.getAttributeName(index);  
  15.             String attributeValue = attrs.getAttributeValue(index);  
  16.             System.out.println("name:" + attributeName + "  value:" + attributeValue);  
  17.         }  
  18.     }  
  19.   
  20. }  
打印结果如下:


AttributeSet:对布局文件XML解析后的结果,封装为AttributeSet对象。存储的都是原始数据,但是对数据进行了简单的加工。

由此构造器帮我们返回了布局文件XML的解析结果,拿到这个结果,我们该怎么做呢?接下来,我们来看看View类对于这个是怎么处理的:

[java] view plaincopyprint?
  1. public View(Context context, AttributeSet attrs) {  
  2.         this(context, attrs, 0);  
  3.     }  
[java] view plaincopyprint?
  1. public View(Context context, AttributeSet attrs, int defStyle) {  
  2.         this(context);  
  3.   
  4.         TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,  
  5.                 defStyle, 0);  
于是,找到一个跟属性很相关的类TypeArray,那么接下来,我在自定义控件的构造方法上也获取一下TypeArray这个类:

翻看一下TypeArray的源码会发现,TypeArray是不继承任何类(除了Object)的,也就是说,TypeArray相当于一个工具类,通过context.obtainStyledAttributes方法,将AttributeSet和属性的类型传递进去,比如AttributeSet相当于原材料,属性类型相当于图纸,context.obtainStyledAttributes相当于加工厂加工成所对象的属性,封装到TypeArray这个类里。

[java] view plaincopyprint?
  1. package com.example.myattrs;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.util.AttributeSet;  
  6. import android.view.View;  
  7.   
  8. public class MyView extends View {  
  9.   
  10.     public MyView(Context context, AttributeSet attrs) {  
  11.         super(context, attrs);  
  12.   
  13.         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView);  
  14.         int count = ta.getIndexCount();  
  15.         for (int i = 0; i < count; i++) {  
  16.             int itemId = ta.getIndex(i);  
  17.             System.out.println("itemId::" + itemId); // 获取属性在R.java文件中的id  
  18.             switch (itemId) {  
  19.             case R.styleable.MyView_test_bitmap:  
  20.                 int bitmapId = ta.getResourceId(itemId, 100);  
  21.                 System.out.println("bitmapId::" + bitmapId);  
  22.                 break;  
  23.             case R.styleable.MyView_test_id:  
  24.                 int test_id = ta.getInteger(itemId, 10);  
  25.                 System.out.println("test_id" + test_id);  
  26.                 break;  
  27.             case R.styleable.MyView_test_msg:  
  28.                 String test_msg = ta.getString(itemId);  
  29.                 System.out.println("test_msg::" + test_msg);  
  30.                 break;  
  31.             default:  
  32.                 break;  
  33.             }  
  34.         }  
  35.     }  
  36.   
  37. }  

以下是TypeArray类里的方法,这里不写注释了,见名知意:


当在构造方法中获取到这些设置好的属性值时,取出其值,就可以在代码中进行处理了。

上篇博客提到了Android自定义控件——仿ios的滑动开关按钮,接下来,就要为这个滑动开关按钮条件自定义的属性,不熟悉上篇博客Demo的,可以先去浏览器一下我的上篇博客,点这里Android自定义控件——仿ios滑动开关按钮

首先,按照上面介绍的步骤,先在res/values目录下创建一个属性文件attrs.xml:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <declare-styleable name="MyToggleBtn">  
  5.   
  6.         <!-- 滑动按钮背景图片 -->  
  7.         <attr name="switchBG" format="reference" />  
  8.         <!-- 滑动块图片 -->  
  9.         <attr name="slideBg" format="reference" />  
  10.         <!-- 设置当前的状态 -->  
  11.         <attr name="currState" format="boolean" />  
  12.     </declare-styleable>  
  13.   
  14. </resources>  
然后,在引用自定义控件的布局文件acticity_main.xml上设置自定义属性,记住,引用这些属性之前,必须先引用命名空间:

xmlns:mytogglebtn="http://schemas.android.com/apk/res/com.example.slidebutton"

其中:mytogglebtn 是任意取名,没有强制要求,但是在控件中引用属性的时候,要保持一致,不要写错了

          com.example.slidebutton 是工程的包名,千万不要弄错了,不然找不到属性文件

[html] view plaincopyprint?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     xmlns:mytogglebtn="http://schemas.android.com/apk/res/com.example.slidebutton"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.   
  7.     <com.example.slidebutton.view.SlideButton  
  8.         android:id="@+id/slidebutton"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerInParent="true"  
  12.         mytogglebtn:currState="false"  
  13.         mytogglebtn:slideBg="@drawable/slide_button_background"  
  14.         mytogglebtn:switchBG="@drawable/switch_background" />  
  15.   
  16. </RelativeLayout>  
有了上面的步骤,我们就可以自定义组件类的构造方法中,将属性集解析成TypeArray了,从TypeArray中获取相关的属性值,并用于初始化自定义控,以下是主要代码:

[java] view plaincopyprint?
  1. public SlideButton(Context context, AttributeSet attrs) {  
  2.         super(context, attrs);  
  3.   
  4.         // 获得自定义属性  
  5.         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyToggleBtn);  
  6.   
  7.         int count = ta.getIndexCount();  
  8.         for (int i = 0; i < count; i++) {  
  9.             int itemId = ta.getIndex(i); // 获取某个属性的Id值  
  10.             switch (itemId) {  
  11.             case R.styleable.MyToggleBtn_currState: // 设置当前按钮的状态  
  12.                 currentState = ta.getBoolean(itemId, false);  
  13.                 break;  
  14.             case R.styleable.MyToggleBtn_switchBG: // 设置按钮的背景图  
  15.                 int backgroundId = ta.getResourceId(itemId, -1);  
  16.                 if (backgroundId == -1)  
  17.                     throw new RuntimeException("资源没有被找到,请设置背景图");  
  18.                 switchBG = BitmapFactory.decodeResource(getResources(), backgroundId);  
  19.                 break;  
  20.             case R.styleable.MyToggleBtn_slideBg: // 设置按钮图片  
  21.                 int slideId = ta.getResourceId(itemId, -1);  
  22.                 if (slideId == -1)  
  23.                     throw new RuntimeException("资源没有找到,请设置按钮图片");  
  24.                 slideButtonBG = BitmapFactory.decodeResource(getResources(), slideId);  
  25.                 break;  
  26.             default:  
  27.                 break;  
  28.             }  
  29.         }  
  30.     }  

        从上可以看到,自定义属性其实很简单。就是在构造方法中,将获取到的属性集加工成TypeArray对象,通过这个对象取出属性的id,通过id取出每个属性对应的值(毕竟Android下的布局文件XML也是key-value形式的),最后将获取到的属性值(控件用户自定义的数据)初始化到自定义控件上,这样,一个完整的自定义控件就完成。这种完整的自定义控件方式用的并不多见,因为在开发自定义控件时候,需要什么数据就直接在Java代码里设置就好了,方便多了。但是在特定的场合下,如果开发的控件某些数据不确定,或者所开发控件需要提供给其他人进行偏好设置什么的,这种自定义属性就显得非用不可了。

0 0
原创粉丝点击