寫(xiě)在前面:
之前這篇文章發(fā)表在我的CSDN博客中, 由于CSDN博客使用過(guò)于繁瑣 ,趁著這段時(shí)間沒(méi)啥事情可做(又要開(kāi)始找新東家了), 將原有的文章整理后發(fā)布到簡(jiǎn)書(shū). 原來(lái)CSDN博客也將停止更新. 有寫(xiě)得不對(duì), 或者有更好的解決方案, 歡迎大家指出.
關(guān)于Android自定義屬性網(wǎng)上已經(jīng)有很多大神都已經(jīng)詳細(xì)的講解過(guò)了. 關(guān)于如何使用自定義屬性大家可以參考鴻洋_大神的這篇 Android 深入理解Android中的自定義屬性 , 在此小弟再補(bǔ)充一些在自定義屬性時(shí)候應(yīng)該注意的一些細(xì)節(jié).
- 如何定義可被多個(gè)自定義控件使用的屬性
- 如何使用
Android
已經(jīng)定義好的屬性 - 各種類(lèi)型的屬性如何定義(文字樣式 , 背景)等
- 如何讀取各種類(lèi)型的自定義屬性
- 如何使用樣式
style
屬性給自定義控件設(shè)置樣式
1. 如何定義可被多個(gè)自定義控件使用的屬性
在編寫(xiě)自定義控件的時(shí)候,經(jīng)常會(huì)遇到兩個(gè)或多個(gè)自定義控件使用同一個(gè)屬性名的情況,假設(shè)有兩個(gè)自定義控件A 和 B ,各自都有屬性attr1
.那么我們?cè)诙xA控件的時(shí)候可以像下面這樣定義attr1
屬性.
<declare-styleable name="CustomView">
<attr name="attr1" format="string"/>
</declare-styleable>
接下來(lái)我們要在另外一個(gè)自定義控件B中使用名稱(chēng)為"attr1
"的自定義屬性,那么我們理所當(dāng)然的也會(huì)像上面這樣定義這個(gè)屬性,那么編譯器會(huì)拋錯(cuò),錯(cuò)誤日志如下:
Error:Execution failed for task ':CustomAttributes:mergeDebugResources'.
> /xxx/xxx/xxx/xxx/xxx/src/main/res/values/attrs.xml: Error: Found item Attr/attr1 more than one time
那么我們?nèi)绾味x一個(gè)能被多個(gè)自定義控件使用的屬性呢?
我們知道在java
類(lèi)中定義一個(gè)被多個(gè)實(shí)例方法共享的變量---將變量定義在方法體外面,因此我們?cè)谧远x屬性時(shí)也可以使用同樣的方法將屬性定義在<declare-styleable></declare-styleable>
標(biāo)簽之外,這樣自定義屬性就可以被多個(gè)自定義控件使用了,代碼如下:
<!-- 將公用的屬性定義在<declare-styleable></declare-styleable>標(biāo)簽之外,即可被多個(gè)自定義控件使用 -->
<attr name="attr1" format="string"/>
2. 如何使用Android已經(jīng)定義好的屬性
Android
系統(tǒng)已經(jīng)為我們定義好了很多屬性名稱(chēng),所有以命名空間android:(xmlns:android="http://schemas.android.com/apk/res/android")
開(kāi)頭的屬性都是Android
已經(jīng)定義好的屬性, 在使用的時(shí)候只需要直接使用即可, 但是應(yīng)該注意的是, 如果這個(gè)屬性在被繼承類(lèi)中已經(jīng)使用過(guò)那么建議不要使用, 如TextView
的android:text
屬性就是已經(jīng)被使用的屬性, 所以在自定義控件時(shí)候如果繼承自TextView
那么這個(gè)屬性就不應(yīng)該被使用.
<declare-styleable name="CustomView">
<!--我們可以按下面三行這樣來(lái)為自定義控件添加,系統(tǒng)已經(jīng)聲明過(guò)的屬性-->
<attr name="android:divider"/>
<attr name="android:dividerPadding"/>
<attr name="android:showDividers"/>
</declare-styleable>
各種類(lèi)型的屬性如何定義
Android
中的屬性是如何定義的, 在使用自定義屬性的時(shí)候我們應(yīng)該了解Android
是如何自定義屬性的.
-
簡(jiǎn)單類(lèi)型的屬性: 像
string
,integer
等類(lèi)型的屬性,我們可以使用<attr format="string" name="屬性名"/>
來(lái)定義 . -
復(fù)合類(lèi)型的屬性: 像
textColor
,background
這樣的復(fù)雜屬性 既可以使用#FF333333
又可以使用@drawable/text_color
類(lèi)型的值.#FF333333
屬于color
類(lèi)型 而@drawable/text_color
屬于reference
類(lèi)型 ,這樣的屬性又改如何定義, 這就需要定義復(fù)合類(lèi)型的屬性, 因此我們可以通過(guò)color| reference
來(lái)定義如:<attr format="color|reference" name="屬性名" />
.
下面是各種類(lèi)型屬性定義說(shuō)明.
屬性類(lèi)型 | 屬性定義方式 | 屬性值說(shuō)明 |
---|---|---|
color | <attr format="color" name="屬性名稱(chēng)"/> | #FF565656 |
string | <attr format="string" name="屬性名稱(chēng)"/> | "字符串" |
integer | <attr format="integer" name="屬性名稱(chēng)"/> | 123 |
boolean | <attr format="boolean" name="屬性名稱(chēng)"/> | true |
fraction | <attr format="fraction" name="屬性名稱(chēng)"/> | 0.9 |
reference | <attr format="reference" name="屬性名稱(chēng)"/> | @drawable/text_color |
dimension | <attr format="dimension" name="屬性名稱(chēng)"/> | 23dp |
enum | <attr format="enum" name="屬性名稱(chēng)"></br> <enum name="horizontal" value="0"/> </br> <enum name="vertical" value="1"/> </br> </attr> | --- |
復(fù)合類(lèi)型 | 由于簡(jiǎn)書(shū)對(duì)表格的支持有問(wèn)題下面的豎線用\/ 表示 |
---- |
TextView的TextColor | <attr format="color/reference" name="屬性名稱(chēng)"/> | 參考color和refernce |
View的Background | <attr format="color/reference" name="屬性名稱(chēng)"/> | 參考color和refernce |
4.如何讀取各種類(lèi)型的自定義屬性
一般步驟:
1. 第一步獲取自定義控件屬性值的集合(TypedArray)
2. 第二步從獲取的集合中取出每個(gè)屬性值
3. 第三步將讀取的屬性值應(yīng)用到自定義控件
1). 獲取自定義控件屬性值的集合
屬性值得集合我們可以通過(guò)context.obtainStyledAttributes
方法來(lái)讀取,首先來(lái)了解一下下面這個(gè)方法
public final TypedArray obtainStyledAttributes(
AttributeSet set, @StyleableRes int[] attrs, @AttrRes int defStyleAttr,
@StyleRes int defStyleRes) {
return getTheme().obtainStyledAttributes(
set, attrs, defStyleAttr, defStyleRes);
}
第二個(gè)參數(shù): 這是一個(gè)數(shù)組,指定我們需要獲取哪些屬性.
第三個(gè)參數(shù): 指定一個(gè)樣式,這里我們指定的是attr.xml定義的樣式屬性
第四個(gè)參數(shù): 指定一個(gè)默認(rèn)的樣式文件.
2). 從獲取的集合中取出每個(gè)屬性值
自定義屬性有 string,integer,color,reference
等簡(jiǎn)單類(lèi)型, 也有 color|reference
等這種復(fù)合類(lèi)型.復(fù)合屬性的讀取需要區(qū)別對(duì)待. 如對(duì)于字體和背景同樣是 color|reference
類(lèi)型,但是他們的讀取方法也有區(qū)別 , 如textColor
一般使用 ColorStateList
類(lèi)型background
一般使用 Drawable
類(lèi)型. 因此需要使用不同的接口來(lái)讀取.
-
ColorStateList
類(lèi)型使用getColorStateList(R.styleable.xxx)
方法; -
Drawable
類(lèi)型使用getDrawable(R.styleable.xxx)
方法; - 簡(jiǎn)單類(lèi)型使用
a.getString(R.styleable.CustomView_CustomString)
方法.
具體介紹請(qǐng)參考下面代碼:
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
final Resources res = getResources();
// 這里能讀取的屬性是在<declare-styleable name="CustomView">定義的屬性
// 這一行獲取 [樣式文件]和[控件屬性] 里面定義的 <declare-styleable name="CustomView"> 標(biāo)簽中的屬性集合
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView, R.attr.CustomStyle, 0);
// 讀取資源文件類(lèi)型的屬性
resource = a.getResourceId(R.styleable.CustomView_CustomResource, 0);
// 讀取color類(lèi)型的屬性(#333333)
color = a.getColor(R.styleable.CustomView_CustomColor, res.getColor(R.color.colorPrimary));
// 讀取文字顏色屬性(#333333或者selector類(lèi)型的drawable資源)
textColor = a.getColorStateList(R.styleable.CustomView_CustomTextColor);
// 讀取背景(#333333或者drawable類(lèi)型資源)注意與文字顏色的區(qū)別
drawable = a.getDrawable(R.styleable.CustomView_CustomBackground);
// 讀取int類(lèi)型屬性
height = a.getInteger(R.styleable.CustomView_CustomHeight, 0);
// 讀取dp類(lèi)型的屬性,讀出來(lái)的值已經(jīng)轉(zhuǎn)換為px
width = a.getDimensionPixelSize(R.styleable.CustomView_CustomWidth, 0);
// 讀取字符串類(lèi)型的屬性
text = a.getString(R.styleable.CustomView_CustomString);
// 讀取枚舉類(lèi)型的屬性
enumValue = a.getInt(R.styleable.CustomView_CustomEnum, 0);
}
- .將讀取的屬性值應(yīng)用到自定義控件
略
如何使用樣式style
屬性給自定義控件設(shè)置樣式
很多時(shí)候我們?cè)陧?xiàng)目開(kāi)發(fā)中都是使用樣式文件對(duì)控件進(jìn)行統(tǒng)一的樣式控制,那自定義控件如何使用自定義樣式呢?
1. 定義自定義控件樣式名稱(chēng)
2. 在style.xml中定義樣式
3. 在主題中設(shè)置自定義控件樣式
4. 在控件布局中設(shè)置自定義控件樣式
1. 定義樣式名稱(chēng)
使用自定義樣式時(shí)候需要先定義樣式名稱(chēng)定義方式如下,這里的名稱(chēng)可以隨意定義,當(dāng)然為了容易識(shí)別最好是以控件名稱(chēng)開(kāi)頭.
<attr format="reference" name="CustomStyle"/>
2. 定義樣式
在style.xml中定義樣式
<style name="myStyleOne">
<item name="CustomColor">#3333</item>
<item name="CustomHeight">34</item>
</style>
3. 使用樣式
樣式的使用可以通過(guò)設(shè)置Activity的主題統(tǒng)一設(shè)置也可以通過(guò)對(duì)單個(gè)控件使用style標(biāo)簽設(shè)置.
<!--通過(guò)主題設(shè)置樣式-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="CustomStyle">@style/myStyleOne</item>
</style>
<!--通過(guò)自定義控件設(shè)置樣式-->
<com.example.customattributes.CustomView
android:layout_width="300dp"
android:layout_height="300dp"
app:CustomColor="#ff0000"
app:CustomString="我的文字"
app:CustomTextColor="@drawable/text_color"
app:CustomWidth="23dp"
app:CustomBackground="@color/colorAccent"
style="@style/myStyleOne"
/>
最后我們?cè)俅位氐角懊嫣岬降姆椒?/p>
public final TypedArray obtainStyledAttributes(
AttributeSet set, @StyleableRes int[] attrs, @AttrRes int defStyleAttr,
@StyleRes int defStyleRes) {
return getTheme().obtainStyledAttributes(
set, attrs, defStyleAttr, defStyleRes);
}
// 讀取int類(lèi)型屬性
height = a.getInteger(R.styleable.CustomView_CustomHeight, 0);
Log.e("CustomView", "高度" + height);
再來(lái)看看第三個(gè)參數(shù)的取值,對(duì)自定義控件屬性值的影響.
取值為0,通過(guò)主題設(shè)置樣式,控制臺(tái)輸出 E/CustomView: 高度0
取值為0,通過(guò)標(biāo)簽中的style
設(shè)置樣式,控制臺(tái)輸出 E/CustomView: 高度34
取值為R.attr.CustomStyle,通過(guò)主題設(shè)置樣式,控制臺(tái)輸出 E/CustomView: 高度34
取值為R.attr.CustomStyle,通過(guò)標(biāo)簽中的style
設(shè)置樣式(設(shè)置為另一個(gè)樣式值),控制臺(tái)輸出 E/CustomView: 高度66
,可以看出第三個(gè)參數(shù)的作用是將指定的樣式應(yīng)用到自定義控件.
下面用一個(gè)表格來(lái)總結(jié)一下obtainStyledAttributes
方法第三個(gè)參數(shù)和樣式設(shè)置之間的關(guān)系.
屬性取值 | 如何設(shè)置樣式 | 控制臺(tái)輸出 | 說(shuō)明 |
---|---|---|---|
空值0 | 主題 | 高度0 | 為0的時(shí)候表示不應(yīng)用主題的樣式值 |
空值0 | 布局文件style屬性 | 高度34 | 設(shè)置style會(huì)將屬性值賦值到對(duì)應(yīng)屬性 |
自定義樣式值R.attr.CustomStyle | 主題 | 高度34 | 主題樣式應(yīng)用到屬性 |
自定義樣式值R.attr.CustomStyle | 主題&布局 | 高度66 | 通過(guò)布局設(shè)置的樣式優(yōu)先級(jí)在主題之上 |