1.自定义属性的意义
上班忙里偷闲写博客,话不多说,我们知道要使用属性的前提是对应的属性应该存在,所以如果要使用我们自己的属性必须先定义才能使用,那么我们应该怎么自定义属性呢?可以查看\sdk\platforms\android-xx\data\res\values目录下找到attrs.xml这个文件,这就是系统自带的所有属性,通过熟悉系统自带的属性规则去自定义属性。我们看一些比较熟悉的:
<declare-styleable name="View">
<attr name="id" format="reference" />
<attr name="background" format="reference|color" />
<attr name="padding" format="dimension" />
...
<attr name="focusable" format="boolean" />
...
</declare-styleable>
<declare-styleable name="TextView">
<attr name="text" format="string" localization="suggested" />
<attr name="hint" format="string" />
<attr name="textColor" />
<attr name="textColorHighlight" />
<attr name="textColorHint" />
...
</declare-styleable>
<declare-styleable name="ViewGroup_Layout">
<attr name="layout_width" format="dimension">
<enum name="fill_parent" value="-1" />
<enum name="match_parent" value="-1" />
<enum name="wrap_content" value="-2" />
</attr>
<attr name="layout_height" format="dimension">
<enum name="fill_parent" value="-1" />
<enum name="match_parent" value="-1" />
<enum name="wrap_content" value="-2" />
</attr>
</declare-styleable>
<declare-styleable name="LinearLayout_Layout">
<attr name="layout_width" />
<attr name="layout_height" />
<attr name="layout_weight" format="float" />
<attr name="layout_gravity" />
</declare-styleable>
<declare-styleable name="RelativeLayout_Layout">
<attr name="layout_centerInParent" format="boolean" />
<attr name="layout_centerHorizontal" format="boolean" />
<attr name="layout_centerVertical" format="boolean" />
...
</declare-styleable>
通过系统自定的attrs.xml文件中的属性,很直接看出他们组织架构的规律,以declare-styleable 为一个组合,后面有一个name属性,属性值为View,TextView等等,可以推理出属性值为View的是View定义的属性,属性值为TextView的就是TextView定义的属性。
我们都知道所有控件的父类都是View,例如TextView理所当然继承了View的所有属性,但是子类也必然存在本身特有的属性,这些扩展的属性只有他自己拥有,比如说View不能使用android:text=”“。
所以说,如果我们在使用自定义控件的时候,如果不为自己的控件扩展一些属性,就只能使用View的属性了。
怎样自定义属性
接下来,模仿系统android:text属性,给我们的自定义控件创建一个文本属性:
第一种:
我们定义一个名为text或者mText的属性(属性名称可以随意起)
<resources>
<declare-styleable name="MyTextView">
<attr name=“text" format="string" />
</declare-styleable>
</resources>
第二种:
我们知道系统已经定义为名为text的属性了,我们不用自己定义,只需要在自定义属性值中申明(需要加上android命名空间,这样才知道使用的是系统的text属性)
<resources>
<declare-styleable name="MyTextView">
<attr name=“android:text"/>
</declare-styleable>
</resources>
那么问题来了,为什么我们使用系统已有的属性还要申明呢?因为text属性是给TextView使用,如果不指明命名空间就不能使用text属性。
属性值的类型format
机智的你一定注意到了第一种方法中出现了format属性,format支持的类型一共有11种:
(1)reference:参考某一资源ID
- 属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
- 属性使用:
<ImageView android:background = "@drawable/图片ID"/>
(2)color:颜色值
- 属性定义:
<attr name = "textColor" format = "color" />
- 属性使用:
<TextView android:textColor = "#00FF00" />
(3)boolean:布尔值
- 自定义属性
<attr name = "focusable" format = "boolean" />
- 属性使用
<Button android:focusable = "true"/>
(4)dimension:尺寸值
- 自定义属性
<attr name = "layout_width" format = "dimension" />
- 属性使用
<Button android:layout_width = "42dip"/>
(5)float:浮点值
- 自定义属性
<attr name = "fromAlpha" format = "float" />
- 属性使用
<alpha android:fromAlpha = "1.0"/>
(6)integer:整型值
- 自定义属性
<attr name = "framesCount" format="integer" />
- 属性使用
<animated-rotate android:framesCount = "12"/>
(7)string:字符串
- 自定义属性
<attr name = "text" format = "string" />
- 属性使用
<TextView android:text = "我是文本"/>
(8)fraction:百分数
- 自定义属性
<attr name = "pivotX" format = "fraction" />
- 属性使用
<rotate android:pivotX = "200%"/>
(9)enum:枚举值
- 自定义属性
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
- 属性使用
<LinearLayout
android:orientation = "vertical">
</LinearLayout>
(10)flag:位或运算
- 自定义属性
<declare-styleable name="名称">
<attr name="gravity">
<flag name="top" value="0x30" />
<flag name="bottom" value="0x50" />
<flag name="left" value="0x03" />
<flag name="right" value="0x05" />
<flag name="center_vertical" value="0x10" />
...
</attr>
</declare-styleable>
- 属性使用
<TextView android:gravity="bottom|left"/>
(11)混合类型:属性定义时可以指定多种类型值
- 自定义属性
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
- 属性使用
<ImageView
android:background = "@drawable/图片ID" />
或者:
<ImageView
android:background = "#00FF00" />
注意:枚举类型的属性在使用的过程中只能同时使用其中一个,不能 android:orientation = “horizontal|vertical”
熟悉了11种类型的属性的使用,但是我们写好布局文件之后,要在控件中使用这些属性的还需要将它解析出来。
4.类中获取属性值
在讲类中获取属性值之前,顺便说一下再不聚文件中如何使用属性。
如果我们自定义属性,这个属性是需要去我们的应用程序包总找的,所以需要引入我们应用程序包的命名空间xmlns:app=”http://schemas.android.com/apk/res-auto”,res-auto表示自动查找,还有一种写法:xmlns:app=”http://schemas.android.com/apk/程序包名”
实战开始啦:
我们先定义一些属性,写好布局文件,创建一个attrs.xml文件,定义自己的属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView">
<!--声明MyTextView需要使用系统定义过的text属性,注意前面需要加上android命名-->
<attr name="android:text" />
<attr name="mTextColor" format="color" />
<attr name="mTextSize" format="dimension" />
</declare-styleable>
</resources>
在布局文件中使用属性:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.openxu.myview.MyTextView
android:layout_width="200dip"
android:layout_height="100dip"
app:mTextSize="25sp"
android:text="我是文字"
app:mTextColor ="#0000ff"
android:background="#ff0000"/>
</LinearLayout>
在自定义view的构造函数中获取属性值:
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
String text = ta.getString(R.styleable.MyTextView_android_text);
int mTextColor = ta.getColor(R.styleable.MyTextView_mTextColor, Color.BLACK);
int mTextSize = ta.getDimensionPixelSize(R.styleable.MyTextView_mTextSize, 100);
ta.recycle(); //注意回收
Log.v("test", “text属性值:"+mText);
Log.v("test", "mTextColor属性值:"+mTextColor);
Log.v("test", "mTextSize属性值:"+mTextSize);
}
至此,自定义属性就完成了。未完待续。。。。