Android 中的 theme 和 style(2)
Drawables
android:selectableItemBackground
android:listChoiceIndicatorSingle
主要提供一些特殊的背景效果,例如 button 在不同狀態的點選效果
Themeception
android:actionBarTheme
android:dialogTheme
這個隨後給大家分享
Styles
是用於定於檢視樣式的一系列的值
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="" android:background="@drawable/my_drawable" />
這裡我們為 ImageView 定義了背景,如果我們想將該背景抽出為 style 以便複用。
<style name="MyStyle"> <item name="android:background"> @drawable/my_drawable </item> </style>
然後修改 ImageView
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="" style="@style/MyStyle" />
Android studio 提供了提取元件樣式來作為 style 使用功能,這樣大大地方便了開發人員。在設計設圖中選擇一個要提取 style 的元件,然後右鍵單擊,在彈出選單中選擇 Refactor 然後選擇 Extract Style... ,

001.JPG
完成上面操作,會看到一個 Extract Android Style 對話中,顯示了該元件所有的可以提取屬性。

002.JPG
我們可以選擇要提取到 style 的屬性,然後 style name 輸入一個 style 名稱這樣單擊 OK 就完成提取

003.JPG
這樣我們在 style.xml 檔案中就可以看到生成的樣式。
style 繼承
<style name="MyStyle"> <item name="android:background"> ?android:attr/selectableItemBackground </item> </style>
這裡? 表示在 theme 進行查詢,android: 表示在 android 名稱空間內進行查詢,attr/表示我們查詢的型別為 attr 這裡可以省略,最後 selectableItemBackground 表示我們要查詢的屬性。