100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 【Android -- Res资源】主题 Theme 的使用总结

【Android -- Res资源】主题 Theme 的使用总结

时间:2023-05-22 17:42:56

相关推荐

【Android -- Res资源】主题 Theme 的使用总结

前言

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);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。