Android ToolBar 解析与应用(三)状态栏,toolbar,窗口背景,底部导航条颜色设置,

本文介绍了如何设置Android应用的状态栏、app bar、navigation bar和窗体背景色。通过在style主题中添加android:windowTranslucentStatus属性实现状态栏透明,设置android:fitsSystemWindows属性配合android:background调整ToolBar背景色,对于navigation bar颜色,需在values-v21/styles.xml中设置,而窗体背景色只需在style.xml中修改activity的主题背景。

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

        如下图所示,能够设置颜色的部分有4个。分别是:状态栏,app bar,navigationbarcolor,窗体背景色.


状态栏透明色:

        1,在style主题样式中添加 android:windowTranslucentStatus 属性;并吧该style设置为activity的theme。

  1. <resources xmlns:tools="http://schemas.android.com/tools">
  2. <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  3. <item name="android:windowTranslucentStatus" tools:targetApi="19">true</item>
  4. </style>
  5. </resources>

        2,在layout中设置  android:fitsSystemWindows 属性,如下。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:fitsSystemWindows="true"
  6. android:orientation="vertical">
  7. <android.support.v7.widget.Toolbar
  8. android:id="@+id/id_toolbar"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:background="#009688"
  12. android:minHeight="?attr/actionBarSize"/>
  13. </LinearLayout>

如此这般,状态栏颜色从原来的黑色,变为透明色了


app bar 背景色设置:

        如上段代码中的android:background="#009688",即可实现toolbar的背景色设置了。很简单吧


navigationbarcolor背景色设置:

        navigationbarcolor是5.0以上系统才会有的,所以相关属性的设置需要在文件(res/values-v21/styles.xml)中。

  1. <resources>>
  2. <style name="AppTheme" parent="AppBaseTheme">
  3. <item name="android:navigationBarColor">#009688</item>
  4. </style>
  5. </resources>

        是不是也很简单?


窗体背景色设置:

        窗体背景色的设置就更加简单了,只需要在style.xml文件的如下设置,并吧该style设置为activity的theme即可。

  1. <resources xmlns:tools="http://schemas.android.com/tools">
  2. <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  3. <item name="android:windowBackground">#00aaff</item>
  4. </style>
  5. </resources>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值