创建Android Material Design的应用主题风格

本文介绍了如何在Android应用中配置Material Design主题样式,包括不同版本的主题样式选择与自定义颜色方案的方法。通过设置AppTheme样式,可以实现统一的应用外观,并确保在不同Android版本上的一致体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 提供了三种Material Design 风格样式

@android:style/Theme.Material (dark version)       
@android:style/Theme.Material.Light (light version)    
@android:style/Theme.Material.Light.DarkActionBar

Light material theme
Light material theme
Dark material theme
Dark material theme

<resources>
  <!-- inherit from the material theme -->
  <style name="AppTheme" parent="android:Theme.Material">
    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->
    <item name="android:colorPrimary">@color/primary</item>
    <!--   darker variant for the status bar and contextual app bars -->
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <!--   theme UI controls like checkboxes and text fields -->
    <item name="android:colorAccent">@color/accent</item>
  </style>
</resources>

我们可以修改每个位置的字或者背景的颜色,每个位置的名字如下图所示:
页面

较低版本使用Material Design风格
要在较低版本上面使用Material Design风格,则需要使用最新的support library(version 21),可以直接把项目引入工程,或者使用gradle构建,增加compile dependency:

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:recyclerview-v7:+'
}

将上面的AppTheme style放到res/values-v21/style.xml,再res/values/style.xml增加一个AppTheme,如下:

<!-- extend one of the Theme.AppCompat themes -->
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    <!-- customize the color palette -->
    <item name="colorPrimary">@color/material_blue_500</item>
    <item name="colorPrimaryDark">@color/material_blue_700</item>
    <item name="colorAccent">@color/material_green_A200</item>
</style>

这样可以同样实现很多的地方是Material Design,但是由于低版本不支持沉浸式状态栏,有一些效果还是无法实现。

参考:http://developer.android.com/intl/zh-cn/training/material/theme.html#StatusBar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值