Material Design的使用

本文介绍如何在 Android 5.X 中应用 Material Design 主题,包括设置主题颜色及阴影效果的方法。通过修改样式文件定义主题样式,并在清单文件中引用。

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

android5.X使用了Material Design主题,现在我们来看看如何使用。
首先在style资源文件中继承android:Theme.Material资源,当然android:Theme.Material...有很多,
然后你设置一下它的一些选项。
<resources>
    <style name="AppTheme" parent="android:Theme.Material">
    <!--导航栏的颜色-->
        <item name="android:colorPrimary">#f20909</item>
    <!--状态栏的颜色-->
        <item name="android:colorPrimaryDark">#0919f2</item>
        <!--导航栏的字体颜色-->
        <item name="android:textColorPrimary">#f5f0f0</item>
    <!--窗口背景的颜色-->
        <item name="android:windowBackground">@color/colorBackground</item>
    <!--底部状态栏的颜色-->
        <item name="android:navigationBarColor">#3ff209</item>
    </style>
</resources>
设置好之后,在清单文件中引用。
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>
就这样设置好主题了。
Material Design的一个重要的特点就是扁平化,可以给view组件加上阴影,有两种方法。
一种是通过android:elevation="xxdp"设置阴影的高度,另一种通过view.setTranslationZ(XXX);来动态设置。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.shuaijie.jiang.mytest.MainActivity">
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent" />
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:background="@color/colorAccent"
        android:elevation="100dp" />

</LinearLayout>

效果如图所示:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值