Android UI之沉浸式状态栏Translucent System Bar

来源:互联网 发布:手写笔软件下载 编辑:程序博客网 时间:2024/06/12 23:44

沉浸式状态栏Translucent System Bar

开发UI中,沉浸式一直是一个问题,如何完美的做到app与界面融为一体,之前写的会有一条很明显的黑线,所以Translucent System Bar 就成了一个完美的工具。这里写图片描述
这样的沉浸式让通知栏和自己定义的toolbar可以融合到一起,做到这个只需要两步:
1.在res/values/styles.xml文件中<style name="ColorTranslucent" parent="AppTheme">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">@color/colorAccent</item>
</style>
定义一种风格,statusbar的颜色设置与toolbar一样即可,在这里我将与单独的一个RelativeLayout的颜色一致就行。

<?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:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:background="@drawable/back"    android:fitsSystemWindows="true"    android:orientation="vertical"    tools:context="com.example.chenxuanhe.translucentsystembar.MainActivity">    <RelativeLayout        android:layout_width="match_parent"        android:background="@color/colorAccent"        android:layout_height="50dp">        <TextView            android:text="adadaddadaadadadadadaddadaad"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </RelativeLayout><TextView    android:text="adadaddadaadadadadadaddadaad"    android:layout_width="wrap_content"    android:layout_height="wrap_content" /></LinearLayout>

android:fitsSystemWindows=”true”这一行必须要,不然会重叠。
其次相对应的布局背景色与风格中的颜色一致即可。
2.在AndroidManifest.xml文件中,将android:theme=”@style/ColorTranslucent”设置在activity中即可。

第二种Translucent System Bar用法:::
这里写图片描述
这种沉浸式用在app是一张背景图的情况下,可以完美的融合。
也只需两步:
1.在res/values/styles.xml中创建风格

  <style name="ImageTranslucent" parent="AppTheme">        <item name="android:windowTranslucentStatus">true</item>        <item name="android:windowTranslucentNavigation">true</item>        <item name="android:statusBarColor">@android:color/transparent</item>    </style>

要注意,需要在AppTheme基础风格上,继承NoActionbar,才能去掉Actionbar,其次,直接在acivity.xml文件中
android:background="@drawable/back"
android:fitsSystemWindows="true"

加入一个背景图,和fitsSystemWindows这一行代码。

2.第二步,在AndroidManifest.xml文件中 更改 android:theme="@style/ImageTranslucent"即可。

注意:在基础风格中需要继承NoActionBar,其次节点文件中需要在activity中去写风格才有用。目前res文件中创建不了values-v19 -v21,能创建,但是显示不了,具体还是不知道为什么。

0 0