1、我们为什么要使用自定义属性
在Android开发的过程中,为了在用户界面上添加Android自带的View,可以在一个xml布局中指定一个View,并通过它相应的元素属性控制该View的外观和行为。
但是,有时候我们需要自己定义一个符合要求的格式、外观或者行为的View,这时候我们就可以通过自定义属性来实现。写得好的自定义View也可以通过xml布局进行添加和设置样式。
2、自定义属性使用的一般步骤
为了实现自定义View的自定义效果,必须按照以下步骤进行实现:
(1) 为用户的View自定义属性;
(2) 为自定义属性指定相应的值;
(3) 在程序运行时取回属性值;
(4) 在你的View中使用你所取回的属性值;
2.1 为用户的View自定义属性
一般情况下,在开发工程中res/valuse/attr.xml文件中,为<resources></resources>添加<declare-styleable>资源元素,并且在其中为用户的View自定义属性。
下面是一个自定义属性的示例:
<resources> <attr name="rightPadding" format="dimension"></attr> <declare-styleable name="SlidingMenu"> <attr name="rightPadding"></attr> </declare-styleable> </resources>
代码声明了一个自定义属性:"rightPadding",属于一个叫做SlidingMenu的样式实体。按照惯例,样式实体的名字是和声明的自定义view类名是相同的。尽管遵循这个惯例不是绝对必要的,但很多有名的代码编写者都基于这个命名惯例来提供声明。
这里Android自定义属性,attr的format取值类型都有什么呢?下面进行常见format取值类型分析(参考:Android中自定义属性的格式详解):
1. reference:参考某一资源ID。
(1)属性定义:
(2)属性使用:<declare-styleable name = "名称"> <attr name = "background" format = "reference" /> </declare-styleable>
<ImageView android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@drawable/图片ID"/>
2. color:颜色值。
(1)属性定义:
(2)属性使用:<declare-styleable name = "名称"> <attr name = "textColor" format = "color" /> </declare-styleable>
<TextView android:layout_width = "42dip" android:layout_height = "42dip" android:textColor = "#00FF00"/>
3. boolean:布尔值
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "focusable" format = "boolean" /> </declare-styleable>
(2)属性使用:
<Button android:layout_width = "42dip" android:layout_height = "42dip" android:focusable = "true"/>
4. dimension:尺寸值
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "layout_width" format = "dimension" /> </declare-styleable>
(2)属性使用:
<Button android:layout_width = "42dip" android:layout_height = "42dip"/>
5. float:浮点值
(1)属性定义:
<declare-styleable name = "AlphaAnimation"> <attr name = "fromAlpha" format = "float" /> <attr name = "toAlpha" format = "float" /> </declare-styleable>
(2)属性使用:
<alpha android:fromAlpha = "1.0" android:toAlpha = "0.7"/>
6. integer:整型值
(1)属性定义:
<declare-styleable name = "AnimatedRotateDrawable"> <attr name = "visible" /> <attr name = "frameDuration" format="integer" /> <attr name = "framesCount" format="integer" /> <attr name = "pivotX" /> <attr name = "pivotY" /> <attr name = "drawable" /> </declare-styleable>
(2)属性使用:
<animated-rotate xmlns:android ="http://schemas.android.com/apk/res/android" android:drawable = "@drawable/图片ID" android:pivotX = "50%" android:pivotY = "50%" android:framesCount = "12" android:frameDuration = "100"/>
7. string:字符串
(1)属性定义:
<declare-styleable name = "MapView"> <attr name = "apiKey" format = "string" /> </declare-styleable>
(2)属性使用:
<com.google.android.maps.MapView android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:apiKey ="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"/>
8. fraction:百分数
(1)属性定义:
<declare-styleable name="RotateDrawable"> <attr name = "visible" /> <attr name = "fromDegrees" format = "float" /> <attr name = "toDegrees" format = "float" /> <attr name = "pivotX" format = "fraction" /> <attr name = "pivotY" format = "fraction" /> <attr name = "drawable" /> </declare-styleable>
(2)属性使用:
<rotate xmlns:android ="http://schemas.android.com/apk/res/android" android:interpolator = "@anim/动画ID" android:fromDegrees = "0" android:toDegrees = "360" android:pivotX = "200%" android:pivotY = "300%" android:duration = "5000" android:repeatMode = "restart" android:repeatCount = "infinite" />
9. enum:枚举值
(1)属性定义:
<declare-styleable name="名称"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable>
(2)属性使用:
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > </LinearLayout>
10. flag:位或运算
(1)属性定义:
<declare-styleable name="名称"> <attr name="windowSoftInputMode"> <flag name = "stateUnspecified" value = "0" /> <flag name = "stateUnchanged" value = "1" /> <flag name = "stateHidden" value = "2" /> <flag name = "stateAlwaysHidden" value = "3" /> <flag name = "stateVisible" value = "4" /> <flag name = "stateAlwaysVisible" value = "5" /> <flag name = "adjustUnspecified" value = "0x00" /> <flag name = "adjustResize" value = "0x10" /> <flag name = "adjustPan" value = "0x20" /> <flag name = "adjustNothing" value = "0x30" /> </attr> </declare-styleable>
(2)属性使用:
<activity android:name = ".StyleAndThemeActivity" android:label = "@string/app_name" android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity>
补充:
属性定义时可以指定多种类型值。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference|color" /> </declare-styleable>
(2)属性使用:
<ImageView android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@drawable/图片ID|#00FF00"/>
2.2 为自定义属性指定相应的值
定义了自定义属性之后,可以在布局XML文件中像内建属性一样使用它们。唯一的不同是,用户自定义属性属于不同的命名空间,需要在布局文件中进行声明。在你的xml布局中添加自定义命名空间xmlns,同时为自定义属性指定相应的值。在最外层的布局文件标签中,添加用户自定义属性的命名空间:
xmlns:custom="http://schemas.android.com/apk/res/[your project package name]"
下面例子显示如何为PieChart使用这些定义过的属性:
为了避免重复使用长的命名空间URI,本例使用了一个xmlns指示符。这个指示指定了别名custom为命名空间http://schemas.android.com/apk/res/com.imooc.slidingmenu。当然了,用户可以为命名空间选择任意的别名。<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:custom="http://schemas.android.com/apk/res/com.imooc.slidingmenu" android:layout_width="match_parent" android:layout_height="match_parent" > <com.imooc.slidingmenu.view.SlidingMenu android:id="@+id/id_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/img_frame_background" custom:rightPadding="80dp" > </com.imooc.slidingmenu.view.SlidingMenu> </RelativeLayout>
注意您用来向布局中添加自定义view的XML标签的名字。这是自定义view类的完全表述。如果您的view内是一个内部类,您必须使用外部类的名字进一步限定它。例如,PieChart类有一个叫做PieView的内部类。为了使用这个类中的自定义属性,您必须使用标签com.example.customviews.charting.PieChart$PieView。
2.3 在程序运行时取回属性值
当view在xml布局中创建了之后,xml标签中所有的属性都从资源包中读取出来并作为一个AttributeSet传递给view的构造函数。
尽管从AttributeSet中直接读取值是可以的,但是这样做有一些缺点:带有值的资源引用没有进行样式处理,所以没有得到允许。
取而代之的是,将AttributeSet传递给obtainStyledAttributes()方法。这个方法传回了一个TypedArray数组,包含了已经解除引用和样式化的值。为了时能能够更容易的调用obtainStyledAttributes()方法,Android资源编译器做了大量的工作。res文件夹中的每个<declare-styleable>资源,生成的R.java都定义了一个属性ID的数组以及一套定义了指向数组中的每一个属性的常量。您可以使用预定义的常量从TypedArray中读取属性。注意:TypedArray对象是一个共享的资源,使用完毕必须回收它。
下例是如何读取这些属性的:
public SlidingMenu(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // 获取我们定义的属性 TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu, defStyle, 0); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.SlidingMenu_rightPadding: mMenuRightPadding = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 50, context .getResources().getDisplayMetrics())); break; } } a.recycle(); }
2.4 在自定义View中使用取回的属性值
在类的其他方法中即可使用取回的值mMenuRightPadding。