R.style https://developer.android.com/reference/android/R.style
樣式和主題 https://developer.android.com/guide/topics/ui/themes
Style resource(有關 XML 中樣式和主題語法的詳細信息) https://developer.android.com/guide/topics/resources/style-resource
R.attr https://developer.android.com/reference/android/R.attr
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
使用:在清單文件的<application>或者<activity>節點里添加
android:theme="@style/AppTheme"
這是AS每次自動為我們添加的代碼。其實Android里面的主題有很多種。
第一講:主題來源
- 系統內置預定義主題
- 兼容包里面預定義主題
1-系統內置預定義主題
1)分類
系統自帶主題:
API 1:
Theme 根主題 灰色標題黑色內容
Theme.Black 背景黑色
Theme.Wallpaper 以桌面墻紙為背景.希望用戶選擇的壁紙出現在窗口后面的主題(API級別10或更低)。
Theme.Light 背景白色
Theme.Translucent 透明背景
Theme.Panel 平板風格
Theme.Dialog 對話框風格
API 11: 3.0
Theme.Holo Holo根主題
Theme.Holo.Black Holo黑主題
Theme.Holo.Light Holo白主題
API 14: 4.0
Theme.DeviceDefault 設備默認根主題
Theme.DeviceDefault.Black 設備默認黑主題
Theme.DeviceDefault.Light 設備默認白主題
API 21: (網上常說的 Android Material Design 就是要用這種主題)
Theme.Material Material根主題
Theme.Material.Light Material白主題
兼容包v7中帶的主題:
Theme.AppCompat 兼容主題的根主題
Theme.AppCompat.Black 兼容主題的黑色主題
Theme.AppCompat.Light 兼容主題的白色主題
根主題包含很多樣式:
android:Theme包含一下很多樣式
<!-- Text styles -->
<!-- Button styles -->
<!-- List attributes -->
<!-- @hide -->
<!-- Gallery attributes -->
<!-- Window attributes -->
<!-- Dialog attributes -->
<!-- AlertDialog attributes -->
<!-- Toast attributes -->
<!-- Panel attributes -->
<!-- Scrollbar attributes -->
<!-- Text selection handle attributes -->
<!-- Widget styles -->
<!-- Preference styles -->
<!-- Search widget styles -->
<!-- Action bar styles -->
<!-- Floating toolbar styles -->
2)語法
兩種寫法:
<style name="AppTheme" parent="android:Theme.Holo.Light"/>
等價于
<style name="AppTheme" parent="@android:style/Theme.Material.Light"/>
2-兼容包里面預定義主題
1.只有一種。Activity要繼承AppCompatActivity才行。這是必要條件。
2.語法:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"/>
第二講:Android全屏
XML控制
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!--全屏-->
<style name="AppTheme.Fullscreen" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
代碼控制
@Override
protected void onCreate(Bundle savedInstanceState) {
// 即隱藏標題欄
requestWindowFeature(Window.FEATURE_NO_TITLE);
//全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
第三講:
@ 和 ? 的區別?
- 使用@表示使用固定的style,而不會跟隨Theme改變,該style可以在對應的style.xml中找到。
- 使用?表示從Theme中查找引用的資源名,這個Google叫做預定義樣式,用在多主題的場景,屬性值會隨著Theme而改變。(?需要和attr配合使用)
style 使用的兩種場景?
<!--使用自定義的style-->
@style/Widget.AppCompat.ProgressBar.Horizontal
<!--使用系統自帶的style-->
@android:style/Widget.ProgressBar.Horizontal
attr 使用的兩種場景
<!--使用自定義的 ,下面兩種方式等效-->
"?attr/屬性"
"?屬性"
<!--使用系統自帶的 ,下面兩種方式等效-->
"?android:屬性"
"?android:attr/屬性"
當引用系統自帶的style和attr時
"@android:style/主題"和"@style/android:主題"等同
"?android:attr/屬性"和"?attr/android:屬性"等同