當我們自定義 View 的時候,至少要定義兩個構造函數(shù)。
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
第二個構造函數(shù)中的參數(shù) attrs 可以獲取在 xml 中添加的屬性的值。
系統(tǒng)已經給我們定義了以下屬性,可以在 sdk/platforms/android-xx/data/res/values/attrs.xml 中找到。我們也可以自定義屬性。
怎么自定義屬性?
- 在 res/values 下創(chuàng)建 attrs.xml 文件
- 添加如下內容
<resources>
<declare-styleable name="CustomTextView">
<attr name="text" format="string" />
</declare-styleable>
</resources>
declare-styleable 定義屬性分組,名稱一般和自定義 View 的名稱一樣。在 R 文件中就會生成 styleable 類。里面包含所有屬性。
public static final class styleable {
public static final int[] CustomTextView = { 2130903853 };
public static final int CustomTextView_text = 0;
}
attr 定義或者聲明屬性。注意,這邊有兩種方式:
- 定義:后面有 format 的就是定義。
- 聲明:后面沒有 format 的就是聲明。比如:
<attr name="android:text"/>。當屬性已經在別的地方定義的時候,不能重復定義,否則會報錯。
- 在 xml 中使用屬性
<com.smaple.CustomTextView
android:layout_width="wrap_content"
android:layout_heidht="wrap_content"
app:text = "Hello World!"
/>
- 在 Java 中獲取屬性值
private void init(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
String text = ta.getString(R.styleable.CustomTextView_text);
// 回收
ta.recycle();
}
Attributeset 就是一個屬性集合,內部是一個 XML 解析器,最后解析成 key-value 的形式。
Java 中獲取系統(tǒng)屬性值
如果要在 Java 中獲取系統(tǒng)的屬性值,必須也要在 attrs.xml 中對應的 View 下聲明。
<resources>
<declare-styleable name="CustomTextView">
<attr name="text" format="string" />
<attr name="android:layout_height" />
</declare-styleable>
</resources>
聲明完了就可以在 Java 中獲取了
int height = ta.getDimensionPixelSize(R.styleable.CustomTextView_android_layout_height, 0)
format 類型
| format | 解釋 |
|---|---|
| integer | 整型值 |
| float | 浮點值 |
| boolean | 布爾值 |
| string | 字符串。比如 "hello"、R.string.hello |
| enum | 枚舉 |
| fraction | 百分數(shù)。只能是 xx% |
| flag | 位或運算 |
| dimension | 尺寸值。比如 10px、10dp、R.dimen.xx |
| color | 顏色值。比如 #FFFFFF、R.color.white |
| reference | 資源 ID。比如 R.drawable.id |
例子:
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView">
<attr name="attr_int" format="integer" />
<attr name="attr_float" format="float" />
<attr name="attr_bool" format="boolean" />
<attr name="attr_string" format="string" />
<attr name="attr_enum">
<enum name="type_1" value="0" />
<enum name="type_2" value="1" />
</attr>
<attr name="attr_fraction" format="fraction" />
<attr name="attr_flag">
<flag name="top" value="0x1" />
<flag name="left" value="0x2" />
<flag name="right" value="0x3" />
<flag name="bottom" value="0x4" />
</attr>
<attr name="attr_dimension" format="dimension" />
<attr name="attr_color" format="color" />
<attr name="attr_reference" format="reference" />
</declare-styleable>
</resources>
activity_main.xml
<com.sample.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:attr_bool="true"
app:attr_color="@color/black"
app:attr_dimension="9dp"
app:attr_enum="type_2"
app:attr_flag="left"
app:attr_reference="@drawable/ic_launcher"
app:attr_float="1.5"
app:attr_fraction="50%"
app:attr_int="1"
app:attr_string="hello" />
CustomView.kt
val ta = context!!.obtainStyledAttributes(attrs, R.styleable.CustomView)
val attrInt = ta.getInt(R.styleable.CustomView_attr_int, 0)
Log.i("tianjf", "attr_int: $attrInt")
val attrFloat = ta.getFloat(R.styleable.CustomView_attr_float, 0f)
Log.i("tianjf", "attr_float: $attrFloat")
val attrBool = ta.getBoolean(R.styleable.CustomView_attr_bool, false)
Log.i("tianjf", "attr_bool: $attrBool")
val attrString = ta.getString(R.styleable.CustomView_attr_string)
Log.i("tianjf", "attr_string: $attrString")
val attrEnum = ta.getInt(R.styleable.CustomView_attr_enum, 0)
Log.i("tianjf", "attr_enum: $attrEnum")
val attrFraction = ta.getFraction(R.styleable.CustomView_attr_fraction, 1, 1, 0f)
Log.i("tianjf", "attr_fraction: $attrFraction")
val attrFlag = ta.getInt(R.styleable.CustomView_attr_flag, 0)
Log.i("tianjf", "attr_flag: $attrFlag")
val attrDimension = ta.getDimension(R.styleable.CustomView_attr_dimension, 0f)
val attrDimensionPixel = ta.getDimensionPixelSize(R.styleable.CustomView_attr_dimension, 0)
Log.i("tianjf", "attr_dimension: $attrDimension")
Log.i("tianjf", "attr_dimension: $attrDimensionPixel px")
val attrColor = ta.getColor(R.styleable.CustomView_attr_color, 0)
Log.i("tianjf", "attr_color: $attrColor")
val attrReference = ta.getDrawable(R.styleable.CustomView_attr_reference)
Log.i("tianjf", "attr_reference: $attrReference")
打印如下:
I/tianjf: attr_int: 1
I/tianjf: attr_float: 1.5
I/tianjf: attr_bool: true
I/tianjf: attr_string: hello
I/tianjf: attr_enum: 1
I/tianjf: attr_fraction: 0.5
I/tianjf: attr_flag: 2
I/tianjf: attr_dimension: 13.5
I/tianjf: attr_dimension: 14 px
I/tianjf: attr_color: -16777216
I/tianjf: attr_reference: android.graphics.drawable.BitmapDrawable@4fe5f57
混合 format 類型
format 還可以添加多個類型,比如系統(tǒng)的 android:background 就是混合類型:
<attr name="background" format="reference|color" />