3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
除了michael的出色答案之外,另一方面是覆蓋主題中的自定義屬性。假設(shè)您有許多自定義視圖,它們都引用自定義屬性“ custom_background”。
<declare-styleable name="MyCustomStylables">
<attr name="custom_background" format="color"/>
</declare-styleable>
在主題中定義值是什么
<style name="MyColorfulTheme" parent="AppTheme">
<item name="custom_background">#ff0000</item>
</style>
要么
<style name="MyBoringTheme" parent="AppTheme">
<item name="custom_background">#ffffff</item>
</style>
您可以使用樣式來(lái)引用屬性
<style name="MyDefaultLabelStyle" parent="AppTheme">
<item name="android:background">?background_label</item>
</style>
注意問(wèn)號(hào),也用于參考android屬性,如
?android:attr/colorBackground
正如大多數(shù)人所注意到的那樣,您可以-并且可能應(yīng)該-使用@color引用而不是硬編碼的顏色。
那為什么不做
<item name="android:background">@color/my_background_color</item>
您無(wú)法在運(yùn)行時(shí)更改“ my_background_color”的定義,而可以輕松切換主題。

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果您的意思是開發(fā)人員可以使用的庫(kù),那么他們將能夠創(chuàng)建自己的主題,并使用主題中的style屬性(customImageButtonStyle
在此答案中)指定小部件的樣式。這就是在Android上自定義操作欄的方式。而且,如果您要在運(yùn)行時(shí)更改主題,那么使用這種方法是不可能的。
- 3 回答
- 0 關(guān)注
- 412 瀏覽
添加回答
舉報(bào)