属性一般定义在 values/attrs.xml 下,如:
<declare-styleable name="window">
<attr name="windowBackground"/>
<attr name="windowNoTitle" />
.....
</declare-styleable>
styleable 在 R.java 中生成了一个int[] 数组,数组的内容是 styleable 属性包含的attr 对应的id 值;
如:
定义
<declare-styleable name="mystyle">
<attr name="textColor" format="dimension" />
<attr name="textSize" format="dimension" />
</declare-styleable>
<attr name="textColor" format="dimension" />
<attr name="textSize" format="dimension" />
</declare-styleable>
<declare-styleable name="orientaion">
<attr name="orientation>
< enum name="horizontal“ value="0"/>
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
format 类型说明:
reference //引用color //颜色dimension //尺寸float //浮点值integer //string //字符串fraction // 百分数枚举型flag
使用:
<RelativeLayout xmlns:android="
http://schemas.android.com/apk/res/android"
xmlns:tools="
http://schemas.android.com/tools"
xmlns:mystyle="
http://schemas.android.com/apk/res/com.example.teststyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.teststyle.MainActivity" >
<com.example.teststyle.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
mystyle:textSize = "@dimen/mydimen"/>
</RelativeLayout>
xmlns:tools="
xmlns:mystyle="
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.teststyle.MainActivity" >
<com.example.teststyle.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
mystyle:textSize = "@dimen/mydimen"/>
</RelativeLayout>
< attr name="windowSoftInputMode">
< flag name = "stateUnspecified" value = "0" />
< flag name = "stateUnchanged" value = "1" />
< flag name = "stateHidden" value = "2" />
< /attr>
< flag name = "stateUnspecified" value = "0" />
< flag name = "stateUnchanged" value = "1" />
< flag name = "stateHidden" value = "2" />
< /attr>
xml 中使用:
android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
这种属性定义一般用于自定义控件。
notice: mystyle 名字不固定;