软件编程
位置:首页>> 软件编程>> Android编程>> Android之沉浸式状态栏的实现方法、状态栏透明

Android之沉浸式状态栏的实现方法、状态栏透明

作者:peterXu00800  发布时间:2022-01-02 17:49:21 

标签:android,沉浸式状态栏

现在越来越多的软件都开始使用沉浸式状态栏了,下面总结一下沉浸式状态栏的两种使用方法

注意!沉浸式状态栏只支持安卓4.4及以上的版本

状态栏:4.4上是渐变色,5.0上是完全透明,本文模拟器为4.4演示

效果图:

Android之沉浸式状态栏的实现方法、状态栏透明

注意!两种方法的区别:

第一种:为顶部栏跟随当前activity的布局文件的背景的颜色,使用方便,不过也有点问题就是,如果有底部虚拟导航键的话,导航键的背景跟顶部的颜色一样,比如:

Android之沉浸式状态栏的实现方法、状态栏透明 

第二种:是通过设置顶部栏的颜色来显示的,可以解决第一种的不足,比如:

Android之沉浸式状态栏的实现方法、状态栏透明

第一种使用方法:

第一、首先在values、values-v19、values-v21文件夹下的styles.xml都设置一个 Translucent System Bar 风格的Theme,如下图:

Android之沉浸式状态栏的实现方法、状态栏透明

values/style.xml:


<style name="TranslucentTheme" parent="AppTheme">
 <!--在Android 4.4之前的版本上运行,直接跟随系统主题-->
</style>

values-v19/style.xml:

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 <item name="android:windowTranslucentStatus">true</item>
 <item name="android:windowTranslucentNavigation">true</item>
</style>

values-v21/style.xml:


<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 <item name="android:windowTranslucentStatus">false</item>
 <item name="android:windowTranslucentNavigation">true</item>
 <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
 <item name="android:statusBarColor">@android:color/transparent</item>
</style>

第二、在清单文件中配置需要沉浸式状态栏的activity加入theme


<activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" />
<activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" />

第三、在Activity的布局文件中的跟布局加入“android:fitsSystemWindows=”true””,但是,这里需要区分一下,就是背景是图片还是纯色:

1.当背景为图片时,布局可以这么写:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/imgs_bj"
 android:fitsSystemWindows="true">

</RelativeLayout>

效果:

Android之沉浸式状态栏的实现方法、状态栏透明

2.当背景为纯色,我们需要对布局划分一下,标题布局与内容布局,先把根布局背景设置成标题布局的背景色,然后标题背景色可以不用设置直接使用根布局的背景色,最后内容布局背景色设置为白色


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/colorPrimary" //根布局背景设置成“标题布局”想要的颜色
 android:fitsSystemWindows="true"
 android:orientation="vertical">

<!--标题布局-->
 <RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="55dp"
   android:background="@color/color_31c27c">

<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerInParent="true"
     android:text="这是标题"
     android:textColor="@android:color/white"
     android:textSize="20sp" />

</RelativeLayout>

<!--内容布局-->
 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/white" //内容区域背景设置成白色
   android:gravity="center"
   android:orientation="vertical">

<Button
     android:layout_marginTop="120dp"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:padding="8dp"
     android:text="显示信息"
     android:onClick="showMsg"
     />
 </LinearLayout>

</LinearLayout>

效果图:

Android之沉浸式状态栏的实现方法、状态栏透明

来源:http://blog.csdn.net/peterxu00800/article/details/54846234

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com