Android-沉浸式导航栏

一、首先看下效果

1.纯色背景情况下

2.图片背景情况下

二、实现方式

2.1,创建StatusBarUtils

package com.example.duqianlong.statusbar;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.v4.graphics.ColorUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by Duqianlong on 2019/10/10.
 */

public class StatusBarUtils {
    public static int getHeight(Context context) {
        int statusBarHeight = 0;
        try {
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                    "android");
            if (resourceId > 0) {
                statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusBarHeight;
    }
    /**
     * 设置状态栏颜色
     * */
    public static void setColor(Context context, @ColorInt int color) {
        if (context instanceof Activity) {
            setColor(((Activity) context).getWindow(), color);
        }
    }
    /**
     * 状态栏字体颜色修改(变深),//防止当状态栏背景颜色太浅太亮的时候,状态栏字体看不清
     * android 6.0 以上支持
     * */
    public static void setTextDark(Context context, boolean isDark) {
        if (context instanceof Activity) {
            setTextDark(((Activity) context).getWindow(), isDark);
        }
    }
    /**
     * 当activity背景是图片时,把图片顶到状态栏显示
     * */
    public static void setTransparent(Context context) {
        if (context instanceof Activity) {
            setTransparent(((Activity) context).getWindow());
        }
    }

    public static void setColor(@NonNull Window window, @ColorInt int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.setStatusBarColor(color);
            setTextDark(window, !isDarkColor(color));
        }

    }
    private static void setTextDark(Window window, boolean isDark) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            View decorView = window.getDecorView();
            int systemUiVisibility = decorView.getSystemUiVisibility();
            if (isDark) {
                decorView.setSystemUiVisibility(systemUiVisibility | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                decorView.setSystemUiVisibility(systemUiVisibility & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        }
    }
    public static void setTransparent(@NonNull Window window) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    public static boolean isDarkColor(@ColorInt int color) {
        return ColorUtils.calculateLuminance(color) < 0.5;
    }
}

2.2,activity调用tuils暴露的方法

package com.example.duqianlong.statusbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置状态栏颜色
        StatusBarUtils.setColor(this, getResources().getColor(R.color.backgroundcolor)); //backgroundcolor:activity背景颜色
        //解决当状态栏背景太亮或太浅的时候 看不清状态栏字体,,,默认:true
        StatusBarUtils.setTextDark(this,true);
        //当背景是图片时,把图片顶到状态栏
        StatusBarUtils.setTransparent(this);
      }
}

此致完事儿

三、附上Git地址供参考

GitHub地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 11 中,实现沉浸导航栏可以通过以下步骤: 1. 在主题中设置 `android:windowTranslucentNavigation` 为 `true`,表示导航栏透明。 2. 在布局文件中设置根布局的 `android:fitsSystemWindows` 为 `true`,表示布局会在系统窗口之上进行绘制,避免内容被导航栏覆盖。 3. 可以通过在 `Activity` 的 `onWindowFocusChanged` 方法中判断导航栏是否显示来动态调整布局。 4. 如果需要在导航栏上添加自定义的内容,可以通过 `WindowManager.LayoutParams` 中的 `FLAG_LAYOUT_IN_SCREEN` 和 `FLAG_LAYOUT_NO_LIMITS` 标志来实现。 下面是一个简单的实现示例: 在 `styles.xml` 文件中设置主题: ```xml <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <item name="android:windowTranslucentNavigation">true</item> </style> ``` 在布局文件中设置根布局: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical"> <!-- 内容布局 --> </LinearLayout> ``` 在 `Activity` 中动态调整布局: ```java @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { View decorView = getWindow().getDecorView(); int uiOptions = decorView.getSystemUiVisibility(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; } else { uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN; } decorView.setSystemUiVisibility(uiOptions); } } ``` 如果需要在导航栏上添加自定义的内容,可以在布局文件中添加: ```xml <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <!-- 内容布局 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:gravity="center" android:text="自定义内容" android:textColor="@android:color/white" android:textSize="18sp" /> </FrameLayout> ``` 然后在 `Activity` 中添加: ```java WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; getWindow().setAttributes(layoutParams); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值