一、Attributeset簡介
Attributeset是Android中一個重要的概念,可以理解為一組XML屬性的集合,它用於在XML布局文件中定義View的屬性。
在View的構造函數中使用AttributeSet參數,可以輕鬆地將XML文件中定義的相關屬性設置到View當中,在代碼中直接調用View的構造函數即可。
Attributeset的使用方式十分靈活,它能夠方便地在不同的View中重用已有的屬性集合,也能添加新的屬性。
二、Attributeset的屬性分類
Attributeset中的屬性可以被分為三類:系統屬性、自定義屬性和樣式屬性。
1. 系統屬性
系統屬性即Android系統內置的屬性,所有View都可以使用系統屬性。這些屬性在View的構造函數中通過AttributeSet參數傳遞,以設置View實例的相關屬性。
比如id、layout_width、layout_height、textColor等屬性。
2. 自定義屬性
自定義屬性是用戶自己定義的屬性,通過在XML文件中使用xmlns屬性定義所屬的命名空間,來將屬性添加到View中。
例如,在res/values/attrs.xml中定義一個自定義命名空間,通過添加一個屬性描述,在XML布局中就可以使用這個新的屬性,來設置View的某些屬性值。
<resources>
<!-- 定義命名空間 -->
<declare-styleable name="MyCustomView">
<!-- 添加一個屬性 -->
<attr name="customAttr" format="boolean" />
</declare-styleable>
</resources>
3. 樣式屬性
樣式屬性是在XML布局文件中設置一系列View屬性的一種方式。
它和自定義屬性的不同在於,它的屬性值可以從其它的樣式中來繼承,簡化了XML布局文件的編寫並使得所有View的屬性更加協調和一致。例如:
<style name="MyStyle">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">18sp</item>
</style>
使用樣式屬性時,可以通過style屬性來指定,在View的構造函數中的AttributeSet中就會包含對應的樣式屬性。
三、Attributeset的使用技巧
1. 在View構造函數設置默認樣式
在View的構造函數中可以設置默認的樣式和屬性,這樣在XML布局文件中僅需要設置需要修改的屬性,就能夠有效地減少XML代碼量,讓代碼更加簡潔易懂。
class MyCustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
//設置默認值
private var attr1 = 0
private var attr2 = 0
init {
attrs?.let {
//讀取自定義屬性
val a = context.obtainStyledAttributes(it, R.styleable.MyCustomView, defStyleAttr, 0)
//設置默認值
attr1 = a.getInt(R.styleable.MyCustomView_customAttr1, 0)
attr2 = a.getInt(R.styleable.MyCustomView_customAttr2, 0)
a.recycle()
}
}
}
2. 通過自定義屬性對View進行擴展
通過自定義屬性,可以為現有的View添加新的功能,這種方式比自定義View要靈活得多。
例如,在一個自定義按鈕中增加一個新的功能,當按鈕被點擊的時候,執行特定的動畫效果:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My Button"
app:animationOnClick="@anim/fade_in_fade_out" />
這裡通過自定義屬性animationOnClick來為按鈕添加了一個新的功能,當點擊按鈕時會執行fadeInFadeOut動畫。
3. 單個屬性與多個屬性的獲取方式
在讀取Attributeset中的屬性時,有兩種方式:單個屬性的獲取方式和多個屬性的獲取方式。
當僅需要獲取單個屬性時,可以使用getAttributeValue方法,例如:getAttrValue(attrs, android.R.attr.background)獲取背景屬性的值。
當需要獲取多個屬性或者屬性組時,可以使用obtainStyledAttributes方法來獲取,例如:
val a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyleAttr, 0)
val customAttr1 = a.getString(R.styleable.MyCustomView_customAttr1)
val customAttr2 = a.getString(R.styleable.MyCustomView_customAttr2)
a.recycle()
四、Attributeset的應用場景
在Android中,應用Attributeset的場景十分廣泛,以下是一些常見的應用場景:
1. 自定義View的屬性設置
當我們需要自定義View的時候,往往需要設置許多屬性,通過Attributeset即可在XML布局文件中設置這些屬性,大大簡化了工作量和代碼複雜度。
2. View的樣式設置
在布局文件中,可以定義樣式屬性來統一設置特定類型的View的屬性,同時可以繼承其他的樣式屬性。
3. AppWidget的屬性設置
AppWidget也是Android中支持Attributeset的地方之一,Attributeset可以保存AppWidget的所有配置屬性,使得AppWidget的開發更加靈活易用。
4. Dialog的界面設計
Dialog是常用的一個組件,它通常需要實現一些自定義界面,Attributeset可以為Dialog界面的組件設置加載樣式和自定義屬性,便於實現細節控制。
5. RecyclerView的子項界面設計
RecyclerView中子項的布局也是可以使用Attributeset的,通過定義自定義的屬性與值來設置複雜的子項界面,同時使得布局更加協調,減少代碼量。
五、代碼示例
布局文件中引用自定義屬性MyCustomView
<com.example.view.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:customAttr1="123"
app:customAttr2="45"/>
MyCustomView.kt自定義View的代碼實現
class MyCustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
//設置默認值
private var attr1 = 0
private var attr2 = 0
init {
attrs?.let {
//讀取自定義屬性
val a = context.obtainStyledAttributes(it, R.styleable.MyCustomView, defStyleAttr, 0)
//設置默認值
attr1 = a.getInt(R.styleable.MyCustomView_customAttr1, 0)
attr2 = a.getInt(R.styleable.MyCustomView_customAttr2, 0)
a.recycle()
}
}
}
自定義屬性的定義方式
<resources>
<!-- 定義命名空間 -->
<declare-styleable name="MyCustomView">
<!-- 添加自定義屬性 -->
<attr name="customAttr1" format="integer" />
<attr name="customAttr2" format="integer" />
</declare-styleable>
</resources>
原創文章,作者:KOUOK,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/370640.html