IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android - 沉浸式状态栏 -> 正文阅读

[移动开发]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;

}

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-01-03 16:13:34  更:2022-01-03 16:13:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 10:53:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码