Android中实现Material3主题

这篇具有很好参考价值的文章主要介绍了Android中实现Material3主题。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

android material3,Android新特性,Android开源框架,android,android jetpack

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>

android material3,Android新特性,Android开源框架,android,android jetpack
请注意,工具栏、按钮和文本的颜色已经改变,现在都使用了我们在Theme.MyAppTheme中定义的相同的绿色。

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

https://m3.material.io/theme-builder#/custom
android material3,Android新特性,Android开源框架,android,android jetpack
我们可以定义我们想要在应用程序中使用的颜色,并且该工具将使用算法创建一个和谐的主题,以确保主要颜色的完美对比。现在,我们所需要做的就是将主题导出为“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效果如下所示:
android material3,Android新特性,Android开源框架,android,android jetpack
如果你想更改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" />

android material3,Android新特性,Android开源框架,android,android jetpack

应用动态着色

通过在我们的应用程序类中简单地添加以下行,我们可以根据用户的系统定义的颜色动态更改应用程序的颜色:
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主题有所帮助!文章来源地址https://www.toymoban.com/news/detail-797887.html

到了这里,关于Android中实现Material3主题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Material Design:为你的 Android 应用提供精美的 UI 体验

    介绍 Material Design 概念:介绍 Material Design 是 Google 推出的一种设计语言,用于创建现代、美观、直观且一致的用户界面。解释 Material Design 的基本原则,包括材料元素、动画、颜色和排版等。 Material Design UI 元素:介绍常用的 Material Design UI 元素,如卡片、按钮、文本字段、图

    2024年02月01日
    浏览(31)
  • 在Fligma打开Android Material 3 Design组件规格颜色大小等

    打开后点击右上方 Open in Figma (提前注册 Figma 账号)

    2024年02月11日
    浏览(33)
  • 如何下载IDEA主题插件Material Theme UI?

    1、打开IDEA插件官网:点击这里,选择IDEA对应的版本下载。我的IDEA版本是2021.3,即我应下载对应的版本6.16.2 2、分别点击IDEA左上角 File - Settings - Plugins - 设置 - install Plugin from Disk… ,从你的电脑里导入刚下载的压缩包Material_Theme_UI-6.16.2 3、( 注意 :路径不能有中文)我的路

    2024年04月26日
    浏览(48)
  • 【1】Pycharm 主题设置推荐Material Theme UI以及编辑环境配置(字体大小和颜色)

    File - Settings - Plugins插件,搜索Material Theme UI 安装。 安装后重启pycharm设置自己喜欢的首选主题。 个人比较喜欢Oceanic主题。 :File - Settings - Editor - Font, Font: Source Code Pro,Size: 16, line-spacing: 1.0,应用。个人觉得这个设置比较舒服 :File - Settings - Editor - Color Scheme Font - General,

    2024年02月16日
    浏览(30)
  • Angular Material

    Angular Material 是一个流行的 UI 组件库,用于在 Angular 应用程序中构建用户界面。它提供了一组预构建的 UI 组件和工具,遵循 Material Design 设计准则,使您能够轻松创建现代和视觉上吸引人的应用程序。 使用 Angular Material,您可以快速向 Angular 项目中添加诸如按钮、卡片、对话

    2024年02月09日
    浏览(24)
  • Unity Material详解

    一、创建 二、属性     1. Shader :Unity内置了一些shader,用户自定义的shader也在这里出现.     Edit : 可以编辑一些shader可编辑的内容,如一些属性. 2. Rendering Mode :渲染模式 Opaque-不透明-石头 适用于所有的不透明的物体 Cutout-镂空-破布 透明度只有0%和100%,不存在半透明的区域。

    2023年04月13日
    浏览(16)
  • Material —— 材质节点 | Coordinates

    目录 1Dto2DIndex 1Dto3DIndex 2Dto1DIndex 3Dto1DIndex ActorPositionWS BlurSampleOffsets BoundingBoxBased_0-1_UVW CameraPositionWS CameraVectorToLatLongUV LightmapUVs LocalPosition LongLatToUV MapARPassthroughCameraUV ObjectAlignedVirtualPlaneCoordinates ObjectOrientation ObjectPositionWS ObjectRadius Panner(P) PanTextureCoordinateChannelfrom-1ton+1 PanT

    2024年02月01日
    浏览(26)
  • Material —— 材质节点 | Utility

    目录 AddNamedRerouteDeclarationNode... AddRerouteNode... AntialiasedTextureMask AtmosphereSunLightIlluminanceOnGround AtmosphereSunLightVector BentNormalCustomOutput BlackBody BoxMask-2D BoxMask-3D BumpOffset(B) ChannelMaskParameter ClearCoatNormalCustomOutput ConstantBiasScale DDX DDY DepthFade DepthOfFieldFunction Desaturation Distance DistanceFieldA

    2024年02月12日
    浏览(24)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包