前言
在AndroidManifest.xml
文件中有<application android:theme="@style/AppTheme">
,其中的 @style/AppTheme 是引用的 res/values/styles.xml 中的主题样式,也有可能是引用的res/values-v11/styles.xml
或者res/values-v14/styles.xml
,这是根据运行此程序的手机系统来决定的,如果手机系统的 API 版本是 11 以上就是v11/styles.xml
,以此类推。在values/styles.xml
中你会发现AppTheme
的主题样式又是继承自AppBaseTheme
,而AppBaseTheme
的父主题就各有不同了,你也可以从这个位置来自己修改主题,此文章主要就是来讨论这个主题如何修改。
Theme 主题的定义
1. 在 AS 中res/values/style.xml
中定义,例如新建项目 AS 自动创建的 Theme,是系统提供;
<!-- 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>
2. 常见的系统主题:
android:theme="@android:style/Theme.Dialog" 将一个Activity显示为能话框模式 android:theme="@android:style/Theme.NoTitleBar" 不显示应用程序标题栏 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不显示应用程序标题栏,并全屏android:theme="Theme.Light" 背景为白色 android:theme="Theme.Light.NoTitleBar" 白色背景并无标题栏 android:theme="Theme.Light.NoTitleBar.Fullscreen" 白色背景,无标题栏,全屏android:theme="Theme.Black" 背景黑色 android:theme="Theme.Black.NoTitleBar" 黑色背景并无标题栏android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏android:theme="Theme.Wallpaper" 用系统桌面为应用程序背景 android:theme="Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景,且无标题栏 android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏 android:theme="Translucent" 半透明 android:theme="Theme.Translucent.NoTitleBar" 半透明、无标题栏 android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 半透明、无标题栏、全屏 android:theme=”Theme.Panel” 半透明,无标题,全屏android:theme=”Theme.Light.Panel”平板风格显示
Theme 主题的使用
在AndroidManifest.xml
为应用或者 Activity 设置 Theme,通过android:theme = "@style/theme
主题名"来引入自己的主题:
<applicationandroid:name=".MyApp"android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".MainActivity"android:theme="@style/AppTheme.Launcher"> //自定义的Theme</application>
在 Java 代码中设置 Theme,**注意:**在 Activity 的onCreate
方法中的setContentView(R.layout.activity_main)
之前设置;
setTheme(R.style.AppTheme);