依賴
implementation 'com.m.k:system-ui:1.0.0-alpha01'
使用方法
SystemBarConfig config = new SystemBarConfig(this).enterFullScreen(SystemBarConfig.MODE_HIDE_LEAN_BACK);
config.apply();
方法
| 方法 | 作用 | |
|---|---|---|
| setStatusBarColor(int color) | 給狀態(tài)欄設(shè)置指定的顏色 | |
| setStatusBarLightMode(boolean lightMode) | 設(shè)置狀態(tài)欄的的模式。 非小米和魅族手機只有 android 6.0 才開始支持,小米和 魅族 4.4 以上即可支持。 true 表示亮色模式,對應(yīng)的狀態(tài)欄字體和圖標就會變?yōu)榛疑粗?false 字體和圖標變成白色 | |
| setContentBehindStatusBar(boolean behind) | true 表示內(nèi)容在狀態(tài)欄后面(不是下面)。false 相反。(注意: 如果 狀態(tài)欄被設(shè)置為隱藏 hideStatusBar = true,并且設(shè)置了 Immersive sticky 模式,那么該方法將會失去作用 默認為contentBehindStatusBar = true 的效果 ) | |
| setNavBarColor(int color) | 給底部導(dǎo)航欄設(shè)置指定的顏色,導(dǎo)航欄的隱藏模式設(shè)置了 immersive sticky ,導(dǎo)航欄顏色無法修改。 | |
| setContentBehindNavBar(boolean behind) | true 表示內(nèi)容在導(dǎo)航欄后面(不是下面)。false 相反。 注意:1. 如果一旦設(shè)置了該屬性為true, 系統(tǒng)會自動設(shè)置內(nèi)容也在狀態(tài)欄后面,即設(shè)置 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 2. 如果 導(dǎo)航欄被設(shè)置為隱藏 hideNavBar = true,并且設(shè)置了 Immersive sticky 模式,那么該方法將會失去作用。默認為contentBehindNavBar = true 的效果 | |
| setHideMode(int mode) | 隱藏系統(tǒng)UI(狀態(tài)欄 + 導(dǎo)航欄)一共有三種模式:1. Lean back 。 用戶按屏幕任意位置, 系統(tǒng)UI 顯示。如果再想顯示,必須重新調(diào)用隱藏方法才能再次隱藏。2. Immersive . 用戶按下狀態(tài)欄并且往下滑動,或者按下導(dǎo)航啦網(wǎng)上滑動 才能顯示 system ui,顯示后必須重新調(diào)用隱藏方法才能再次隱藏。3. Immersive sticky 和 第二種模式一樣,只是 幾秒種后會自動再次隱藏,并且設(shè)置為false 無效 | |
| setHideStatusBar(boolean hide) | 是否隱藏狀態(tài)欄 。true 隱藏,false 不隱藏 | |
| setHideNavBar(boolean hide) | 是否隱藏底部導(dǎo)航欄。true 隱藏,false 不隱藏 | |
| keepLayoutStable(boolean stable) | 是否保持布局穩(wěn)定,當 狀態(tài)欄和導(dǎo)航欄在顯示和隱藏狀態(tài)之間切換時,是否保持布局大小不變。 |
注:沉浸式模式下,修改狀態(tài)欄和導(dǎo)航欄顏色無效。如果模式為該模式,有一下情況:
- 如果 設(shè)置了 hideStatusBar = true , 那么 contentBehindStatusBar 默認為true, 即使調(diào)用 contentBehindStatusBar(false) 也沒用
- 如果 設(shè)置了 hideNavBar = true ,那么 contentBehindNavBar 默認為true ,即使調(diào)用了 contentBehindNavBar(false) 也沒用
SystemBarConfig類
package com.m.k.systemui;
import android.app.Activity;
import com.m.k.systemui.uitils.Logger;
import com.m.k.systemui.uitils.SystemFacade;
public class SystemBarConfig {
// https://developer.android.google.cn/training/system-ui/immersive
public static final int MODE_IMMERSIVE = 0x101;
// 并且該模式下即使 contentBehindStatusBar 和 contentBehindNavBar 為false,一旦 導(dǎo)航欄顯示時,內(nèi)容也是在導(dǎo)航欄后面。這點和 其他兩個模式不一樣。
/**
* 該模式下 修改狀態(tài)欄和導(dǎo)航欄顏色無效,
* 如果模式為該模式,有一下情況:
* 1. 如果 設(shè)置了 hideStatusBar = true , 那么 contentBehindStatusBar 默認為true, 即使調(diào)用 contentBehindStatusBar(false) 也沒用
* 2. 如果 設(shè)置了 hideNavBar = true ,那么 contentBehindNavBar 默認為true ,即使調(diào)用了 contentBehindNavBar(false) 也沒用
*/
public static final int MODE_IMMERSIVE_STICKY = 0x103;
public static final int MODE_HIDE_LEAN_BACK = 0x104;
public static final int NO_COLOR = -2;
private Activity activity;
private int statusBarColor = NO_COLOR;
private int navBarColor = NO_COLOR;
private boolean contentBehindStatusBar = false;
private boolean contentBehindNavBar = false;
// true 狀態(tài)欄字體為黑色,false 狀態(tài)欄字體為白色
private boolean statusBarLightMode = false;
private boolean hideStatusBar = false;
private boolean hideNavBar = false;
private boolean isKeepLayoutStable = false;
private int hideMode = MODE_HIDE_LEAN_BACK;
private boolean isUseNotch = true;
private boolean isHideSystemUi;
public SystemBarConfig(Activity activity) {
this.activity = activity;
}
/**
* 給狀態(tài)欄設(shè)置指定的顏色
* @param color
* @return
*/
public SystemBarConfig setStatusBarColor(int color) {
this.statusBarColor = color;
return this;
}
/**
* 設(shè)置狀態(tài)欄的的模式。 非小米和魅族手機只有 android 6.0 才開始支持,小米和 魅族 4.4 以上即可支持
* @param lightMode true 表示亮色模式,對應(yīng)的狀態(tài)欄字體和圖標就會變?yōu)榛疑?,反?false 字體和圖標變成白色
* @return
*/
public SystemBarConfig setStatusBarLightMode(boolean lightMode) {
this.statusBarLightMode = lightMode;
return this;
}
/**
*
* @param behind : true 表示內(nèi)容在狀態(tài)欄后面(不是下面)。false 相反.
* 注意: 如果 狀態(tài)欄被設(shè)置為隱藏 hideStatusBar = true,并且設(shè)置了 Immersive sticky 模式,那么該方法將會失去作用 默認為contentBehindStatusBar = true 的效果
* @return
*/
public SystemBarConfig setContentBehindStatusBar(boolean behind) {
this.contentBehindStatusBar = behind;
return this;
}
/**
* 給底部導(dǎo)航欄設(shè)置指定的顏色,導(dǎo)航欄的隱藏模式設(shè)置了 immersive sticky ,導(dǎo)航欄顏色無法修改。
* @param color
* @return
*/
public SystemBarConfig setNavBarColor(int color){
this.navBarColor = color;
return this;
}
/**
*
* @param behind : true 表示內(nèi)容在導(dǎo)航欄后面(不是下面)。false 相反
* 注意:1. 如果一旦設(shè)置了該屬性為true, 系統(tǒng)會自動設(shè)置內(nèi)容也在狀態(tài)欄后面,即設(shè)置 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
* 2. 如果 導(dǎo)航欄被設(shè)置為隱藏 hideNavBar = true,并且設(shè)置了 Immersive sticky 模式,那么該方法將會失去作用。默認為contentBehindNavBar = true 的效果
* @return
*/
public SystemBarConfig setContentBehindNavBar(boolean behind){
this.contentBehindNavBar = behind;
if(behind){
Logger.w("讓內(nèi)容顯示在導(dǎo)航欄的后面,會默認自動設(shè)置內(nèi)容也顯示在狀態(tài)欄后面");
}
return this;
}
/**
* 隱藏系統(tǒng)UI(狀態(tài)欄 + 導(dǎo)航欄)一共有三種模式,<p />
*
* 1. Lean back 。 用戶按屏幕任意位置, 系統(tǒng)UI 顯示。如果再想顯示,必須重新調(diào)用隱藏方法才能再次隱藏 <p />
* 2. Immersive . 用戶按下狀態(tài)欄并且往下滑動,或者按下導(dǎo)航啦網(wǎng)上滑動 才能顯示 system ui,顯示后必須重新調(diào)用隱藏方法才能再次隱藏<p />
* 3. Immersive sticky 和 第二種模式一樣,只是 幾秒種后會自動再次隱藏,
* 并且設(shè)置為false 無效
*
* @param mode
* @return
*/
public SystemBarConfig setHideMode(int mode){
this.hideMode = mode;
return this;
}
/**
* 是否隱藏狀態(tài)欄
* @param hide ,true 隱藏,false 不隱藏
* @return
*/
public SystemBarConfig setHideStatusBar(boolean hide){
this.hideStatusBar = hide;
return this;
}
/**
* 是否隱藏底部導(dǎo)航欄
* @param hide ,true 隱藏,false 不隱藏
* @return
*/
public SystemBarConfig setHideNavBar(boolean hide){
this.hideNavBar = hide;
return this;
}
/**
* 是否保持布局穩(wěn)定,當 狀態(tài)欄和導(dǎo)航欄在顯示和隱藏狀態(tài)之間切換時,是否保持布局大小不變。
* @param stable
* @return
*/
public SystemBarConfig keepLayoutStable(boolean stable){
this.isKeepLayoutStable = stable;
return this;
}
public SystemBarConfig enterFullScreen(int mode){
isHideSystemUi =true;
hideMode = mode;
return this;
}
public SystemBarConfig existFullScreen(){
contentBehindNavBar =true;
contentBehindStatusBar = true;
isUseNotch = true;
return this;
}
public SystemBarConfig setUseNotch(boolean useNotch) {
isUseNotch = useNotch;
return this;
}
boolean isUseNotch() {
return isUseNotch;
}
int getNavBarColor() {
return navBarColor;
}
boolean isContentBehindNavBar() {
return contentBehindNavBar;
}
boolean isHideStatusBar() {
return hideStatusBar;
}
boolean isHideNavBar() {
return hideNavBar;
}
int getHideMode() {
return hideMode;
}
boolean isKeepLayoutStable(){
return isKeepLayoutStable;
}
public void apply() {
if(isHideSystemUi){ // 只要設(shè)置了進入全屏,強制設(shè)置一下值得行為
hideStatusBar = true;
hideNavBar = true;
contentBehindNavBar =true;
contentBehindStatusBar = true;
isUseNotch =true;
if(getHideMode() == MODE_IMMERSIVE_STICKY){
statusBarColor = NO_COLOR;
navBarColor = NO_COLOR;
}
}
ISystemUiAdapter statusBarAdapter = null;
if (SystemFacade.hasP()) {
statusBarAdapter = new PieUiAdapter(this);
} else if (SystemFacade.hasO()) {
statusBarAdapter = new OreoUiAdapter(this);
} else if (SystemFacade.hasM()) {
statusBarAdapter = new MarshmallowUiAdapter(this);
} else if (SystemFacade.hasLollipop()) {
statusBarAdapter = new LollipopUIAdapter(this);
} else if (SystemFacade.hasKitKat()) {
statusBarAdapter = new KitKatUiAdapter(this);
}
if (statusBarAdapter != null) {
statusBarAdapter.apply();
}
}
Activity getActivity() {
return activity;
}
int getStatusBarColor() {
return statusBarColor;
}
boolean isContentBehindStatusBar() {
return contentBehindStatusBar;
}
boolean isStatusBarLightMode() {
return statusBarLightMode;
}
public static SystemBarConfig create(Activity activity){
return new SystemBarConfig(activity);
}
}