Android 沉浸式状态栏

本文介绍了Android4.4及以上版本中实现沉浸式状态栏的三种方法:通过代码设置、利用工具类动态添加View以及使用开源库SystemBarTint。每种方法都详细展示了具体的实现步骤。

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

沉浸式状态的应用,是android4.4加入的功能实现。

先看看应用的实例(状态栏的颜色改变了,整个界面和谐了):

     

实现方法有三种:

1,系统实现

在代码中进行设置:

public class TopBarActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        setContentView(R.layout.layout_topbar);
    }
}
在布局中加入申明:fitsSystemWindows="true" , clipToPadding="true"

<?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">

    <TextView
        android:fitsSystemWindows="true"
        android:clipToPadding="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textColor="@android:color/white"
        android:background="@android:color/holo_blue_dark"
        android:text="topbar"/>

</LinearLayout>

2,在Android4.4加入 WindowStanlucentStatus这个属性后,Android的StatusBar这个区域时可用的,于是我们可以在根布局

动态添加一个与状态栏等高的View,并设置背景色。

下面是一个工具类:

package com.example.songbinwang.liveinhand.uitls;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;

import java.lang.reflect.Field;

/**
 * Created by songbinwang on 2016/10/20.
 */

public class StatusBarCompat {

    public static final int Color_Default = Color.parseColor("#20000000");

    public static void compat(Activity activity){
        compat(activity, Color_Default);
    }

    public static void compat(Activity activity, int statusColor){

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            if(statusColor != -1){
                activity.getWindow().setStatusBarColor(statusColor);
            }
        }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
            if(statusColor != -1){
                ViewGroup convertView = (ViewGroup) activity.findViewById(android.R.id.content);
                int statusHeight = getStatusBarHeight(activity);
                View statusView= new View(activity);
                statusView.setBackgroundColor(statusColor);
                ViewGroup.LayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusHeight);
                convertView.addView(statusView, lp);
            }
        }
    }

    /**
     * 获取状态栏的高度
     * @return
     */
    protected static int getStatusBarHeight(Context context){
        try
        {
            Class<?> c=Class.forName("com.android.internal.R$dimen");
            Object obj=c.newInstance();
            Field field=c.getField("status_bar_height");
            int x=Integer.parseInt(field.get(obj).toString());
            return  context.getResources().getDimensionPixelSize(x);
        }catch(Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
Activity调用(在这里我没有使用,getWindow().addFlags(int)方法,因为android5.0之后,getWidow().setStatusBarColor()相冲突):

public class TopBarActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ){
            StatusBarCompat.compat(this, Color.parseColor("#ff0099cc"));
        }

        setContentView(R.layout.layout_topbar);
    }
}

在values-v19对应(Android4.4)中的styles.xml中添加属性<item name="fitsSystemWindows">true</item> 。 values-v21/styles.xml中去掉(values-21对应Android5.0)

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:<span style="color: rgb(63, 63, 63); font-family: 'Source Code Pro', monospace; font-size: 12.6px; line-height: 35px; white-space: nowrap; background-color: rgba(128, 128, 128, 0.0745098);">windowTranslucentStatus</span>">true</item> <!--values-v19/styles.xml-->
    </style>

第三种方法是一个开源库。 SystemBarTint

其实它的的核心类就一个:SystemBarTint-master\library\src\com\readystatesoftware\systembartint\SystemBarTintManager.java

我们只要把这个类拷贝到你的项目中,就可以使用:

public class TopBarActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ){
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            //激活状态栏
            tintManager.setStatusBarTintEnabled(true);
            //激活导航栏
            tintManager.setNavigationBarTintEnabled(true);
            //设置颜色
            tintManager.setStatusBarTintColor(getResources().getColor(android.R.color.holo_orange_dark));
            tintManager.setNavigationBarTintColor(getResources().getColor(android.R.color.holo_green_dark));
        }
        setContentView(R.layout.layout_topbar);
    }
}

布局文件:

<?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:fitsSystemWindows="true"
    android:clipToPadding="true">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textColor="@android:color/white"
        android:background="@android:color/holo_blue_dark"
        android:text="topbar"/>

</LinearLayout>

展示一下三种方法测试的效果图:图一为,第一种方法和第二种方法测试的效果图,图二为第三种方法的测试效果图。

     















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值