IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android style(样式) 和 theme(主题) 属性 -> 正文阅读

[移动开发]Android style(样式) 和 theme(主题) 属性

1. 定义

????????样式和主题资源都可用于对Android UI组件进行“美化”,只要充分利用好这两个属性资源,我们可以开发出各种风格的应用界面。

????????style 样式: 一个样式相当于多个格式的集合,其他UI组件通过style属性来指定样式,样式资源文件时放在/res/values/styles.xml目录下,其根元素为<resources...../> , 该元素内可包含多个<style..../>子元素,每个 <style.../>子元素定义一个样式。

<style..../>元素指定如下两个属性

????????name : 指定样式的名称

????????parent : 指定继承父类的样式:

<resources>
    <style name="system.theme.test" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

????????theme 主题:与样式资源相似,主题资源的xml文件也是放在/res/values/themes.xml,? XML文件同样根元素为<resources...../>,同样使用<style..../>元素来定义主题

<resources>
    <style name="Theme.DeskClock.Settings">
        <!-- Attributes from androidx.appcompat.appcompat -->
        <item name="actionBarStyle">@style/Widget.ActionBar</item>
        <item name="colorControlActivated">?attr/colorAccent</item>
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">false</item>
</resources>

2. 区别

? ? ? ? theme主题一般是作用于整个 Application 或 某个 Activity

? ? ? ? style 样式一般作用于某个控件,比如Button Layout等

3. 使用注意点

? ? ? ? 主题在新建项目的时候在AndroidManifest.xml 中一般默认配置了

<application
        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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>

? ? 假如我们把?android:theme="@style/AppTheme" 去掉,就会报如下错误:

? ? 你必须要给这个app设定一个主题

E AndroidRuntime: Process: com.example.themeactivitytest, PID: 28833
E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.themeactivitytest/com.example.themeactivitytest.MainActivit
y}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

? ? 如果你不想在AndoridManifest.xml文件中配置主题,也可以在onCreate()方法中通过代码设定

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //步骤1. 设置主题
        setTheme(R.style.Theme_NoTitleBar_Fullscreen);
        //步骤2. 加载布局
        setContentView(R.layout.activity_main);
    }

如果这两个步骤顺序写反了,也会报:AndroidRuntime: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 错误

4. 常用的主题

常见的主题风格有:
android:theme="@android:style/Theme.Dialog"  灰色背景 将一个Activity显示为对话框模式

android:theme="@android:style/Theme.NoTitleBar" 灰色背景 不显示应用程序标题栏

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 灰色背景,不显示应用程序标题栏,并全屏

android:theme="@android:style/Theme.Light" 背景为白色

android:theme="@android:style/Theme.Light.NoTitleBar" 白色背景并无标题栏

android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 白色背景,无标题栏,全屏

android:theme="@android:style/Theme.Black" 背景黑色

android:theme="@android:style/Theme.Black.NoTitleBar" 黑色背景并无标题栏

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏

android:theme="@android:style/Theme.Wallpaper" 用系统桌面为应用程序背景

android:theme="@android:style/Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景,且无标题栏

android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏

android:theme="@android:style/Theme.Translucent"  半透明

android:theme="@android:style/Theme.Translucent.NoTitleBar" 半透明、无标题栏

android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" 半透明、无标题栏、全屏

android:theme="@android:style/Theme.Panel"   面板风格显示

如果配置为? ?android:theme="@android:style/Theme.NoDisplay"? 此界面不显示,一般用于Activity传递纯数据,不需要界面显示的场景

需要注意事项就是需要在onResume 生命周期方法之前调用finish() 方法结束掉此Activity,否则报错:E AndroidRuntime: Caused by: java.lang.IllegalStateException: Activity? did not call
finish() prior to onResume() completing

5. 更多主题

? ? ? ? 可以查看源码? ? ? ? ?样式 和? 主题路径:

? ? ? framework/base/core/res/res/values/themes.xml

? ? ? framework/base/core/res/res/values/themes_material.xml? ? ? ?M- design风格的

? ? ? framework/base/core/res/res/values/styles.xml ?

? ? ? framework/base/core/res/res/values/styles_material.xml? ? ? ??M- design风格的

android:theme="@android:style/Theme.Material.NoActionBar.Fullscreen"   M风格 无标题,全屏主题

android:theme="@android:style/Theme.Material.Light.Dialog"    Material风格 白色 Dialog主题

android:theme="@android:style/Theme.Material.Wallpaper"    Material风格 背景为壁纸的主题

android:theme="@android:style/Theme.Material.Settings"     Material风格类似设置模块主界面
    

比较类似,根据实际需求配置主题

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-06-16 21:48:29  更:2022-06-16 21:49:02 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/22 7:49:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码