Android中实现Material3主题

news2024/7/6 18:02:33

Android中实现Material3主题

Material 3是由Google引入的一种设计系统,通过采用一套设计原则、指南和组件,提供统一直观的用户体验。

在本篇文章中,您将学习如何:

  • 在您的Android应用程序中应用Material 3主题。
  • 如何使用Material 3属性应用于您的视图。
  • 如何应用动态着色。

开始

首先需要引入material组件以来:

dependencies {
    // ...
    implementation 'com.google.android.material:material:1.9.0'
}

创建material3主题theme.xml

<style name="Theme.MyAppTheme" parent="Theme.Material3.Light">
   <item name="colorPrimary">@color/green</item>
   <item name="colorPrimaryVariant">@color/green_dark</item>
   <item name="colorOnPrimary">@color/white</item>
</style>

我们可以通过将它们添加到我们刚创建的主题中,来更改默认颜色的值。在这里,您可以查看 Material 3 的所有角色并决定要覆盖哪种颜色。

https://github.com/material-components/material-components-android/blob/master/docs/theming/Color.md

请注意,所有 Material 3 组件都使用 Material 3 样式,因此如果我们更改主题值,我们的视图将自动受到影响。

应用

AndroidManifest.xml中引入theme

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.MyAppTheme"> // change here
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

下面是主界面布局activity_main.xml,看看效果

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/username"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="96dp"
        android:autofillHints="@string/prompt_email"
        android:hint="@string/prompt_email"
        android:inputType="textEmailAddress"
        android:selectAllOnFocus="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:autofillHints="@string/prompt_password"
        android:hint="@string/prompt_password"
        android:imeActionLabel="@string/action_sign_in_short"
        android:imeOptions="actionDone"
        android:inputType="textPassword"
        android:selectAllOnFocus="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/username" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="64dp"
        android:text="@string/action_sign_in"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/password"
        app:layout_constraintVertical_bias="0.2" />
</androidx.constraintlayout.widget.ConstraintLayout>


请注意,工具栏、按钮和文本的颜色已经改变,现在都使用了我们在Theme.MyAppTheme中定义的相同的绿色。

但是,如果我们能以简单和和谐的方式定义所有Material 3主题的值会怎样呢?这就是Material 3主题创建工具发挥作用的地方。

https://m3.material.io/theme-builder#/custom
主题生成工具
我们可以定义我们想要在应用程序中使用的颜色,并且该工具将使用算法创建一个和谐的主题,以确保主要颜色的完美对比。现在,我们所需要做的就是将主题导出为“Android视图(XML)”。

该工具将导出适用于浅色和深色主题的值,包括所有的颜色。

theme.xml(浅色主题)

<resources>
    <style name="Theme.MyAppTheme" parent="Theme.Material3.Light.NoActionBar">
        <item name="colorPrimary">@color/md_theme_light_primary</item>
        <item name="colorOnPrimary">@color/md_theme_light_onPrimary</item>
        <item name="colorPrimaryContainer">@color/md_theme_light_primaryContainer</item>
        <item name="colorOnPrimaryContainer">@color/md_theme_light_onPrimaryContainer</item>
        <item name="colorSecondary">@color/md_theme_light_secondary</item>
        <item name="colorOnSecondary">@color/md_theme_light_onSecondary</item>
        <item name="colorSecondaryContainer">@color/md_theme_light_secondaryContainer</item>
        <item name="colorOnSecondaryContainer">@color/md_theme_light_onSecondaryContainer</item>
          <! -- [...] a lot of attrs here -->
    </style>
</resources>

colors.xml

<resources>
    <color name="seed">#6750A4</color>
    <color name="md_theme_light_primary">#6750A4</color>
    <color name="md_theme_light_onPrimary">#FFFFFF</color>
    <color name="md_theme_light_primaryContainer">#EADDFF</color>
    <color name="md_theme_light_onPrimaryContainer">#21005D</color>
    <color name="md_theme_light_secondary">#625B71</color>
    <color name="md_theme_light_onSecondary">#FFFFFF</color>
    <color name="md_theme_light_secondaryContainer">#E8DEF8</color>
      <! -- [...] a lot of colors here -->
</resources>

现在app UI效果如下所示:

如果你想更改button的颜色,非常简单

<Button
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:backgroundTint="?attr/colorAccent" // Use the prefix ?attr
    android:layout_marginTop="16dp"
    android:layout_marginBottom="64dp"
    android:text="@string/action_sign_in"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/password"
    app:layout_constraintVertical_bias="0.2" />

应用动态着色

通过在我们的应用程序类中简单地添加以下行,我们可以根据用户的系统定义的颜色动态更改应用程序的颜色:
MyApplication.kt

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        DynamicColors.applyToActivitiesIfAvailable(this)
    }
}

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:name=".MyApplication" 
    android:theme="@style/Theme.MyAppTheme"> 

以上就是本文全部内容,希望对你学习material3主题有所帮助!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/699030.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

10.1寸工业三防平板应用于工业自动化生产

随着工业自动化的不断发展&#xff0c;工业生产中对于设备的要求也越来越高。在恶劣的工作环境中&#xff0c;工业设备需要具备防尘、防水、防震等功能&#xff0c;以确保设备的稳定运行和长期使用。10.1寸工业三防平板作为一种重要的工业自动化设备&#xff0c;广泛应用于各个…

【算法】算法效率分析 -- 时间空间复杂度

文章目录 概述时间复杂度常数阶 O ( 1 ) O(1) O(1)线性阶 O ( n ) O(n) O(n)对数阶 O ( l o g n ) O(logn) O(logn)线性对数阶 O ( n ∗ l o g n ) O(n*logn) O(n∗logn)次方阶 O ( n 2 ) O(n^2) O(n2) O ( n 3 ) O(n^3) O(n3) O ( n k ) O(n^k) O(nk) O ( n m ) O(nm) …

保持无损连接的BCNF分解算法

建议在看之前熟悉候选键的求法&#xff0c;不清楚的可以转到这里来&#xff1a; http://t.csdn.cn/fW30Q 步骤&#xff1a; INPUT:关系模式R以及在R上成立的函数依赖集F 1.初始化P{R} 2.若P中的所有关系模式S都是BCNF&#xff0c;则转步骤(4) 3.若P中有一个模式S不是BCNF&am…

Linux——IP协议2

目录 公网IP ​编辑 特殊的IP地址 IP地址的数量限制 私有IP地址和公网IP地址 路由 数据链路层 认识以太网 以太网帧格式 如何解包和封装&#xff0c;交付及分用 重谈局域网通信原理 认识MTU MTU对于TCP协议的影响 查看硬件地址和MTU ARP协议 模拟ARP请求 …

ros2内结合gazebo和rviz进行yolov8检测记录

前提&#xff1a;第一次接触ros2, 遇到的问题解决方式不一定准确&#xff0c;只是这次我尝试成功了&#xff0c;想和大家分享一下。 ubuntu20.04系统 目录 1. ros2 1.1 ros2是啥&#xff1f; 1.2 ros2的版本和ubuntu版本的对应关系&#xff0c;当下入门尤其是ubuntu20.04系…

【物理摩擦力图像】对摩擦力大小的因素研究图像

摩擦力与编程 两个相互接触并挤压的物体&#xff0c;当它们发生相对运动或具有相对运动趋势时&#xff0c;就会在接触面上产生阻碍相对运动或相对运动趋势的力&#xff0c;这种力叫做摩擦力&#xff08;Ff或f&#xff09;。 摩擦力与正压力&#xff08;B物体上的A物体产生的压…

数据库实验—复杂查询

查询20161151班的学生在大学一年级选修的课程情况&#xff0c;查询结果要显示学号(Sno)、姓名(Sname)、专业名(Mname)、选课的课程号(Cno)、选课的课程名称(Cname)及成绩&#xff08;Grade)&#xff0c;并按照学号、课程号升序排序 select Sno, Sname,Mname, Cno,Cname,Grade f…

Windows11安装WSL2(Ubuntu20.04)

以管理员身份打开 PowerShell&#xff0c;输入以下命令安装&#xff1a; wsl --install dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /no…

【历史上的今天】6 月 29 日:SGI 和 MIPS 合并;微软收购 PowerPoint 开发商;新闻集团出售 Myspace

整理 | 王启隆 透过「历史上的今天」&#xff0c;从过去看未来&#xff0c;从现在亦可以改变未来。 今天是 2023 年 6 月 29 日&#xff0c;在 2008 年的今天&#xff0c;CNNIC 发布《第 22 次中国互联网络发展状况统计报告》&#xff0c;截至 2008 年 6 月底&#xff0c;中国…

draggable里包裹的卡片,卡片里有个input,点击input聚焦无效。

在input标签上加pointerdown.stop.native <el-input placeholder"请输入" pointerdown.stop.native v-model"dataForm.nickName" :style"{width:180px}" suffix-icon"el-icon-search" lazy />

TLD7002学习笔记(三)-使用S32K144EVB烧录TLD7002

文章目录 1. 前言2. 烧录数据准备2.1 OTP Wizard的下载与安装2.2 OTP Wizard的配置2.3 OTP寄存器烧录数据提取 3. OTP烧录和仿真的流程3.1 OTP烧录流程3.2 OTP仿真流程 4. 验证测试4.1 测试代码4.2 测试环境4.3 测试情况 5. 参考资料 1. 前言 本篇文章是TLD7002学习笔记的第三…

设计模式第18讲——中介者模式

目录 一、什么是中介者模式 二、角色组成 三、优缺点 四、应用场景 4.1 生活场景 4.2 java场景 五、代码实现 5.0 代码结构 5.1 抽象中介者&#xff08;Mediator&#xff09;——LogisticsCenter 5.2 抽象同事类&#xff08;Colleague&#xff09;——Participant 5…

Beego之Bee安装(Windows)以及创建,运行项目

一.简介 Bee是什么&#xff1f; bee工具是一个为了协助快速开发 Beego 项目而创建的项目&#xff0c;通过 bee 可以很容易的进行 Beego 项目的 创建、热编译、开发、测试和部署 Beego中文文档 Beego中文文档: Beego简介 安装前提 在安装bee之前&#xff0c;首先得提前安装好Go的…

【T3】打开财务报表提示不能登陆到服务器,请检查服务器配置。

【问题描述】 在使用畅捷通T3软件的时候&#xff0c; 打开【财务报表】提示&#xff1a;不能登陆到服务器&#xff08;GUPR7FM&#xff09;&#xff0c;请检查服务器配置。 但是打开【总账系统】&#xff0c;填制凭证、查看报表等操作都正常。 【解决方法】 由于操作【总账系…

【Java高级编程】Java反射机制

Java反射机制 1、反射的概述1.1、本章的主要内容1.2、关于反射的理解1.3、体会反射机制的“动态性”1.4、反射机制能提供的功能1.5、相关API 2、Class类的理解与获取Class的实例2.1、Class类的理解2.2、获取Class实例的几种方式2.3、总结&#xff1a;创建类的对象的方式2.4、Cl…

生产者与消费者问题

本篇文章我们使用C探讨一下生产者与消费者问题. 1. 多线程的引入 我们学习了操作系统, 知道了进程和线程的概念, 但是如果不进行代码实战的话, 会很难理解它们. 特别是编程的初学者(比如我), 在了解了进程和线程后通常会感到疑惑: 多线程怎么用? 为啥我平时写代码没有使用到…

ATM机项目实战——准备

项目介绍 大概功能&#xff1a; 实现一个模拟ATM机存款、取款、转账功能的一个系统&#xff0c;可以查看打印交易明细&#xff0c;后台用户可以管理用户账户卡信息。 适合人群&#xff1a; 在校计算机专业的大学生&#xff0c;愿意从事JAVA开发的人群&#xff0c;具体基础的…

记一次系统的jar包本地化方案

重在思路 行内容器环境,tomcat版本是8.5,导致jar包冲突,优先调用了jar包中的方法,致使同名同路径下,改写的类方法失效,报java.lang.NoSuchMethodError错误,删除对应class后需要重新更新jar包到行内maven私服仓库,流程复杂,且不清楚哪些地方依然有重写方法的地方,设置tomcat加载…

基于SSM+jsp的二手车交易网站设计与实现

博主介绍&#xff1a; 大家好&#xff0c;我是一名在Java圈混迹十余年的程序员&#xff0c;精通Java编程语言&#xff0c;同时也熟练掌握微信小程序、Python和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我擅长在JavaWeb、SSH、SSM、SpringBoot等框架…

DBeaver mysql socks5 代理

DBeaver mysql socks5 代理 测试连接报错 Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.切换代理&#xff0c;连接成功 参考 DBeaver 文档 代理配置