Android - 沉浸式状态栏

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“com.mythmayor.a1805statusbardemo.Status4Activity”>

<ImageView

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:scaleType=“centerCrop”

android:src="@mipmap/mythmayor_bg03" />

  • Java代码

package com.mythmayor.a1805statusbardemo;

import android.os.Build;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

public class Status4Activity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_status4);

}

@Override

public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

if (hasFocus && Build.VERSION.SDK_INT >= 19) {

View decorView = getWindow().getDecorView();

decorView.setSystemUiVisibility(

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);

}

}

}

方式2.透明状态栏效果,需要在5.0及其以上系统才支持。

  • 布局文件
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“com.mythmayor.a1805statusbardemo.Status2Activity”>

<ImageView

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:scaleType=“centerCrop”

android:src="@mipmap/mythmayor_bg03" />

  • Java代码

package com.mythmayor.a1805statusbardemo;

import android.graphics.Color;

import android.os.Build;

import android.os.Bundle;

import android.support.v7.app.ActionBar;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

public class Status2Activity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_status2);

if (Build.VERSION.SDK_INT >= 21) {

View decorView = getWindow().getDecorView();

int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;

decorView.setSystemUiVisibility(option);

getWindow().setStatusBarColor(Color.TRANSPARENT);

}

ActionBar actionBar = getSupportActionBar();

actionBar.hide();

}

}

方式3.将状态栏和导航栏设置为透明

  • Step1 在布局文件根布局中加入下面代码

android:fitsSystemWindows=“true”

  • Step2 在Activity文件的setContent()方法之前加入下面代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

//透明状态栏

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

//透明导航栏

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

}

方式4.将状态栏和导航栏设置为透明

  • Step1 在布局文件根布局中加入下面代码

android:fitsSystemWindows=“true”

  • Step2 使用SystemBarTintManager和StatusBarUtil对状态栏进行设置
只需一行代码即可设置指定颜色的沉浸式状态栏

/** 设置沉浸式状态栏 */

StatusBarUtil.initSystemBar(this, R.color.white);

下面附上代码
SystemBarTintManager.java

import android.annotation.SuppressLint;

import android.annotation.TargetApi;

import android.app.Activity;

import android.content.Context;

import android.content.res.Configuration;

import android.content.res.Resources;

import android.content.res.TypedArray;

import android.graphics.drawable.Drawable;

import android.os.Build;

import android.util.DisplayMetrics;

import android.util.TypedValue;

import android.view.Gravity;

import android.view.View;

import android.view.ViewConfiguration;

import android.view.ViewGroup;

import android.view.Window;

import android.view.WindowManager;

import android.widget.FrameLayout.LayoutParams;

import java.lang.reflect.Method;

public class SystemBarTintManager {

static {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

try {

Class c = Class.forName(“android.os.SystemProperties”);

Method m = c.getDeclaredMethod(“get”, String.class);

m.setAccessible(true);

sNavBarOverride = (String) m.invoke(null, “qemu.hw.mainkeys”);

} catch (Throwable e) {

sNavBarOverride = null;

}

}

}

/**

  • The default system bar tint color value.

*/

public static final int DEFAULT_TINT_COLOR = 0x99000000;

private static String sNavBarOverride;

private final SystemBarConfig mConfig;

private boolean mStatusBarAvailable;

private boolean mNavBarAvailable;

private boolean mStatusBarTintEnabled;

private boolean mNavBarTintEnabled;

private View mStatusBarTintView;

private View mNavBarTintView;

/**

  • Constructor. Call this in the host activity onCreate method after its

  • content view has been set. You should always create new instances when

  • the host activity is recreated.

  • @param activity The host activity.

*/

@TargetApi(19)

@SuppressWarnings(“ResourceType”)

public SystemBarTintManager(Activity activity) {

Window win = activity.getWindow();

ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

// check theme attrs

int[] attrs = {android.R.attr.windowTranslucentStatus,

android.R.attr.windowTranslucentNavigation};

TypedArray a = activity.obtainStyledAttributes(attrs);

try {

mStatusBarAvailable = a.getBoolean(0, false);

mNavBarAvailable = a.getBoolean(1, false);

} finally {

a.recycle();

}

// check window flags

WindowManager.LayoutParams winParams = win.getAttributes();

int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

if ((winParams.flags & bits) != 0) {

mStatusBarAvailable = true;

}

bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;

if ((winParams.flags & bits) != 0) {

mNavBarAvailable = true;

}

}

mConfig = new SystemBarConfig(activity, mStatusBarAvailable, mNavBarAvailable);

// device might not have virtual navigation keys

if (!mConfig.hasNavigtionBar()) {

mNavBarAvailable = false;

}

if (mStatusBarAvailable) {

setupStatusBarView(activity, decorViewGroup);

}

if (mNavBarAvailable) {

setupNavBarView(activity, decorViewGroup);

}

}

/**

  • Enable tinting of the system status bar.

  • If the platform is running Jelly Bean or earlier, or translucent system

  • UI modes have not been enabled in either the theme or via window flags,

  • then this method does nothing.

  • @param enabled True to enable tinting, false to disable it (default).

*/

public void setStatusBarTintEnabled(boolean enabled) {

mStatusBarTintEnabled = enabled;

if (mStatusBarAvailable) {

mStatusBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);

}

}

/**

  • Enable tinting of the system navigation bar.

  • If the platform does not have soft navigation keys, is running Jelly Bean

  • or earlier, or translucent system UI modes have not been enabled in either

  • the theme or via window flags, then this method does nothing.

  • @param enabled True to enable tinting, false to disable it (default).

*/

public void setNavigationBarTintEnabled(boolean enabled) {

mNavBarTintEnabled = enabled;

if (mNavBarAvailable) {

mNavBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);

}

}

/**

  • Apply the specified color tint to all system UI bars.

  • @param color The color of the background tint.

*/

public void setTintColor(int color) {

setStatusBarTintColor(color);

setNavigationBarTintColor(color);

}

/**

  • Apply the specified drawable or color resource to all system UI bars.

  • @param res The identifier of the resource.

*/

public void setTintResource(int res) {

setStatusBarTintResource(res);

setNavigationBarTintResource(res);

}

/**

  • Apply the specified drawable to all system UI bars.

  • @param drawable The drawable to use as the background, or null to remove it.

*/

public void setTintDrawable(Drawable drawable) {

setStatusBarTintDrawable(drawable);

setNavigationBarTintDrawable(drawable);

}

/**

  • Apply the specified alpha to all system UI bars.

  • @param alpha The alpha to use

*/

public void setTintAlpha(float alpha) {

setStatusBarAlpha(alpha);

setNavigationBarAlpha(alpha);

}

/**

  • Apply the specified color tint to the system status bar.

  • @param color The color of the background tint.

*/

public void setStatusBarTintColor(int color) {

if (mStatusBarAvailable) {

mStatusBarTintView.setBackgroundColor(color);

}

}

/**

  • Apply the specified drawable or color resource to the system status bar.

  • @param res The identifier of the resource.

*/

public void setStatusBarTintResource(int res) {

if (mStatusBarAvailable) {

mStatusBarTintView.setBackgroundResource(res);

}

}

/**

  • Apply the specified drawable to the system status bar.

  • @param drawable The drawable to use as the background, or null to remove it.

*/

@SuppressWarnings(“deprecation”)

public void setStatusBarTintDrawable(Drawable drawable) {

if (mStatusBarAvailable) {

mStatusBarTintView.setBackgroundDrawable(drawable);

}

}

/**

  • Apply the specified alpha to the system status bar.

  • @param alpha The alpha to use

*/

@TargetApi(11)

public void setStatusBarAlpha(float alpha) {

if (mStatusBarAvailable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

mStatusBarTintView.setAlpha(alpha);

}

}

/**

  • Apply the specified color tint to the system navigation bar.

  • @param color The color of the background tint.

*/

public void setNavigationBarTintColor(int color) {

if (mNavBarAvailable) {

mNavBarTintView.setBackgroundColor(color);

}

}

/**

  • Apply the specified drawable or color resource to the system navigation bar.

  • @param res The identifier of the resource.

*/

public void setNavigationBarTintResource(int res) {

if (mNavBarAvailable) {

mNavBarTintView.setBackgroundResource(res);

}

}

/**

  • Apply the specified drawable to the system navigation bar.

  • @param drawable The drawable to use as the background, or null to remove it.

*/

@SuppressWarnings(“deprecation”)

public void setNavigationBarTintDrawable(Drawable drawable) {

if (mNavBarAvailable) {

mNavBarTintView.setBackgroundDrawable(drawable);

}

}

/**

  • Apply the specified alpha to the system navigation bar.

  • @param alpha The alpha to use

*/

@TargetApi(11)

public void setNavigationBarAlpha(float alpha) {

if (mNavBarAvailable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

mNavBarTintView.setAlpha(alpha);

}

}

/**

  • Get the system bar configuration.

  • @return The system bar configuration for the current device configuration.

*/

public SystemBarConfig getConfig() {

return mConfig;

}

/**

  • Is tinting enabled for the system status bar?

  • @return True if enabled, False otherwise.

*/

public boolean isStatusBarTintEnabled() {

return mStatusBarTintEnabled;

}

/**

  • Is tinting enabled for the system navigation bar?

  • @return True if enabled, False otherwise.

*/

public boolean isNavBarTintEnabled() {

return mNavBarTintEnabled;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值