本文主要介紹如何為自定義的View添加屬性以及屬性的類型。代碼示例定義見DropDownToRefreshListView,調用見DropDownToRefreshListViewDemo
1、添加自定義View的屬性文件
在res/values中新建attrs.xml文件(文件名可另取,不過推薦用attrs.xml,可以將自定義屬性都放入其中),內容為
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="myViewDefinedAttr"> <attr name="attr1" format="boolean"/> <attr name="attr2" format="integer"/> </declare-styleable> </resources>
定義名為myViewDefinedAttr的屬性列表,這個name命名也可以用下划線形式。name會在下面第二步中使用。
<attr name="attr1" format="boolean"/> 其中name為屬性名,format為屬性類型,可以為boolean, string, integer, dimension, float, reference, color, fraction, enum, flag及其混合。具體定義及調用方式見本文最后關於自定義屬性的介紹。
2、自定義View中獲取屬性值
public class MyView extends View { private boolean attr1; private int attr2; public MyView(Context context){ super(context); } public MyView(Context context, AttributeSet attrs){ super(context, attrs); getAttrs(context, attrs); } public MyView(Context context, AttributeSet attrs, int defStyle){ super(context, attrs, defStyle); getAttrs(context, attrs); } /** * 得到屬性值 * * @param context * @param attrs */ private void getAttrs(Context context, AttributeSet attrs) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myViewDefinedAttr); attr1 = ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true); attr2 = ta.getInt(R.styleable.myViewDefinedAttr_attr2, 0); ta.recycle(); } }
從上面可以看出,主要是通過context.obtainStyledAttributes得到屬性及其值的對應列表。
ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true);表示得到屬性名為attr1的boolean值,若不存在該屬性,則默認為true。這里的R.styleable.myViewDefinedAttr_attr1為第一步中的屬性列表名_屬性名。
ta.recycle();為回收系統資源。
3、調用自定義View
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage" android:layout_width="match_parent" android:layout_height="match_parent" > <com.trinea.android.common.view.MyView android:id="@+id/app_list" android:layout_width="match_parent" android:layout_height="match_parent" android:fastScrollEnabled="true" myViewXmlns:attr1="false" myViewXmlns:attr2="1" android:focusable="true" /> …… </RelativeLayout>
調用的主體為<com.trinea.android.common.view.MyView ……/>中內容,需要注意的是跟上面代碼類似在外面的布局文件中加入自己的命名空間再通過命名空間調用屬性,即
xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage",其中myViewXmlns為空間名可自取,值為http://schemas.android.com/apk/res/加當前應用的包名,即AndroidManifest.xml中manifest節點的package值,如下
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trinea.mypackage"
4、自定義屬性的類型
format表示的屬性類型可以為boolean, string, integer, dimension, float, reference, color, fraction, enum, flag及其混合。
(1) boolean表示布爾值,調用如 xx:attr1="false"
(2) integer表示整型,調用如 xx:attr1="1"
(3) dimension表示尺寸值,調用如 xx:attr1="42dp"
(4) float表示浮點型,調用如 xx:attr1="0.7"
(5) color表示顏色值,調用如 xx:attr1="#00FF00"
(6) string表示字符串,調用如 xx:attr1="#adbddd"
(7) reference表示參考某一資源id,調用如 xx:attr1 = "@drawable/圖片ID"
(8) fraction表示百分數,調用如 xx:attr1="30%"
以上類型定義都為<attr name="attr1" format="xxxtype"/>
(9) enum表示枚舉值,定義為
<attr name="enum_attr"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr>
調用如 xx:attr1="horizontal"
(10) flag表示位或運算,定義為
<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>
調用如:xx:attr1="stateUnspecified | stateUnchanged | stateHidden"
(11) 混合類型,定義為
<declare-styleable name = "combine_type"> <attr name = "background" format = "reference|color" /> </declare-styleable>
調用如 xx:attr1 = "@drawable/圖片ID|#DDFF00"