style 和theme

1,style 和 theme 的區別:

When you apply a style to a single View in the layout, the properties defined by the style are applied only to that View. If a style is applied to a ViewGroup, the child View elements will not inherit the style properties—only the element to which you directly apply the style will apply its properties. However, you can apply a style so that it applies to all View elements—by applying the style as a theme.html

To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest. When you do so, every View within the Activity or application will apply each property that it supports. For example, if you apply the CodeFont style from the previous examples to an Activity, then all View elements that support the text style properties will apply them. Any View that does not support the properties will ignore them. If a View supports only some of the properties, then it will apply only those properties.android

2,引用樣式屬性app

Referencing style attributeside

(@ 與 ? 的區別)工具

您能夠經過樣式屬性資源在當前應用的風格主題中引用某個屬性的值。 經過引用樣式屬性,您能夠不採用爲 UI 元素提供硬編碼值這種方式,而是經過爲 UI 元素設置樣式,使其匹配當前風格主題提供的標準變型來定製這些元素的外觀。引用樣式屬性的實質做用是,「在當前風格主題中使用此屬性定義的樣式」。ui

要引用樣式屬性,名稱語法幾乎與普通資源格式徹底相同,只不過將 at 符號 (@) 改成問號 (?),資源類型部分爲可選項。 例如:編碼

?[<package_name>:][<resource_type>/]<resource_name>

例如,您能夠經過如下代碼引用一個屬性,將文本顏色設置爲與系統風格主題的「主要」文本顏色匹配:spa

<EditText id="text"
   
android:layout_width="fill_parent"
   
android:layout_height="wrap_content"
   
android:textColor="?android:textColorSecondary"
   
android:text="@string/hello_world" />

在以上代碼中,android:textColor 屬性表示當前風格主題中某個樣式屬性的名稱。Android 如今會使用應用於 android:textColorSecondary 樣式屬性的值做爲 android:textColor 在這個小工具中的值。因爲系統資源工具知道此環境中確定存在某個屬性資源,所以您無需顯式聲明類型(類型應爲 ?android:attr/textColorSecondary)— 您能夠將 attr 類型排除在外。code

相關文章
相關標籤/搜索