Android實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色的變化實(shí)例代碼詳解
今天介紹一下,我在項(xiàng)目開發(fā)過程中,實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色變化的方法,實(shí)現(xiàn)方式是,通過隱藏系統(tǒng)的狀態(tài)欄和虛擬按鍵的背景,實(shí)現(xiàn)圖片和背景顯示到狀態(tài)欄和虛擬按鍵下方。下面來看實(shí)現(xiàn)代碼:
實(shí)現(xiàn)狀態(tài)欄背景的設(shè)置
狀態(tài)欄工具類
public class StatusBarUtil {
/**
* 設(shè)置沉浸式狀態(tài)欄
*
* @param activity 需要設(shè)置的activity
*/
public static void setTransparent(Activity activity) {
//API19一下不考慮
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
transparentStatusBar(activity);
setStatusBarTextColor(activity, Color.WHITE);
}
/**
* 使?fàn)顟B(tài)欄透明
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private static void transparentStatusBar(Activity activity) {
Window window = activity.getWindow();
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);
//設(shè)置虛擬按鍵背景透明,同時(shí)該屬性會(huì)實(shí)現(xiàn)沉浸式狀態(tài)欄
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.setStatusBarColor(Color.TRANSPARENT);
// window.setNavigationBarColor(Color.BLACK);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* Android 6.0 以上設(shè)置狀態(tài)欄顏色
*/
protected static void setStatusBarTextColor(Activity activity, @ColorInt int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 如果亮色,設(shè)置狀態(tài)欄文字為黑色
if (isLightColor(color)) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}
/**
* 判斷顏色是不是亮色
*
* @param color
* @return
* @from https://stackoverflow.com/questions/24260853/check-if-color-is-dark-or-light-in-android
*/
private static boolean isLightColor(@ColorInt int color) {
return ColorUtils.calculateLuminance(color) >= 0.5;
}
/**
* 將布局設(shè)置為狀態(tài)欄的高度
*
* @param context
* @param view
*/
public static void setStatusBarHeight(Context context, View view) {
// 獲得狀態(tài)欄高度
int height = getStatusBarHeight(context);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = height;
view.setLayoutParams(layoutParams);
// status_bar.requestLayout();//請(qǐng)求重新布局
}
/**
* 獲取狀態(tài)欄高度
*
* @param context context
* @return 狀態(tài)欄高度
*/
public static int getStatusBarHeight(Context context) {
// 獲得狀態(tài)欄高度
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
return context.getResources().getDimensionPixelSize(resourceId);
}
}
調(diào)用方式(在super.onCreate(savedInstanceState)方法之前調(diào)用):
StatusBarUtil.setTransparent(this);
該方法中,首先判斷API版本,由于API19以下沒有設(shè)置狀態(tài)欄的方法,所以我們只考慮19以上的版本,接著調(diào)用了transparentStatusBar()方法,根據(jù)API21為分界,分別實(shí)現(xiàn)狀態(tài)欄背景的透明,然后是調(diào)用setStatusBarTextColor()方法,設(shè)置狀態(tài)欄字體的顏色。
實(shí)現(xiàn)效果:
1、沉浸式

2、自定義狀態(tài)欄,我設(shè)置的背景為白色

如果要填充自己需要的導(dǎo)航欄顏色的話,可以自己創(chuàng)建一個(gè)導(dǎo)航欄布局layout_head,
<?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="wrap_content" android:background="@color/bgGray" android:orientation="vertical"> <View android:id="@+id/status_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"/> </LinearLayout>
通過以下代碼:
protected View getHeadView() {
View view = View.inflate(activity, R.layout.layout_head, null);
View status_bar = view.findViewById(R.id.status_bar);
//status_bar .setBackground()
StatusBarUtil.setStatusBarHeight(activity, status_bar);
return view;
}
// frameLayout是你的activity留出的狀態(tài)欄布局
frameLayout.addView(getHeadView());
這樣,就可以設(shè)置自己想要的狀態(tài)欄的顏色和高度了。
虛擬按鍵背景顏色的設(shè)置
虛擬按鍵工具類
public class NavigationBarUtil {
public static void initActivity(View content) {
new NavigationBarUtil(content);
}
/**
* 被監(jiān)聽的視圖
*/
private View mObserved;
/**
* 視圖變化前的可用高度
*/
private int usableHeightView;
private ViewGroup.LayoutParams layoutParams;
private NavigationBarUtil(View content) {
mObserved = content;
//給View添加全局的布局監(jiān)聽器監(jiān)聽視圖的變化
mObserved.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
resetViewHeight1();
}
});
layoutParams = mObserved.getLayoutParams();
}
private int usableHeight = 0;
private void resetViewHeight1() {
int usableHeightViewNow = CalculateAvailableHeight();
//比較布局變化前后的View的可用高度
InputMethodManager inputMethodManager = (InputMethodManager) VankeApplication.getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
Rect rect = new Rect();
mObserved.getWindowVisibleDisplayFrame(rect);
usableHeight = Math.max(usableHeight, rect.bottom);
if (inputMethodManager.isActive() && usableHeight > rect.bottom) {//軟鍵盤顯示,導(dǎo)致界面布局改變
return;
}
if (usableHeightViewNow != usableHeightView) {
//如果兩次高度不一致
//將當(dāng)前的View的可用高度設(shè)置成View的實(shí)際高度
Configuration mConfiguration = VankeApplication.getApplication().getResources().getConfiguration(); //獲取設(shè)置的配置信息
int ori = mConfiguration.orientation; //獲取屏幕方向
if (ori == Configuration.ORIENTATION_LANDSCAPE) {
//橫屏
layoutParams.width = usableHeightViewNow;
} else if (ori == Configuration.ORIENTATION_PORTRAIT) {
//豎屏
layoutParams.height = usableHeightViewNow;
}
mObserved.requestLayout();//請(qǐng)求重新布局
usableHeightView = usableHeightViewNow;
}
}
/**
* 計(jì)算試圖高度
*
* @return
*/
private int CalculateAvailableHeight() {
Rect r = new Rect();
mObserved.getWindowVisibleDisplayFrame(r);
Configuration mConfiguration = VankeApplication.getApplication().getResources().getConfiguration(); //獲取設(shè)置的配置信息
int ori = mConfiguration.orientation; //獲取屏幕方向
if (ori == Configuration.ORIENTATION_LANDSCAPE) {
//橫屏
return (r.right);
} else if (ori == Configuration.ORIENTATION_PORTRAIT) {
//豎屏
return (r.bottom);
}
// return (r.bottom - r.top);//如果不是沉浸狀態(tài)欄,需要減去頂部高度
return (r.bottom);//如果是沉浸狀態(tài)欄
}
/**
* 判斷底部是否有虛擬鍵
*
* @param context
* @return
*/
public static boolean hasNavigationBar(Context context) {
boolean hasNavigationBar = false;
Resources rs = context.getResources();
int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasNavigationBar = rs.getBoolean(id);
}
try {
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method m = systemPropertiesClass.getMethod("get", String.class);
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
hasNavigationBar = false;
} else if ("0".equals(navBarOverride)) {
hasNavigationBar = true;
}
} catch (Exception e) {
}
return hasNavigationBar;
}
}
調(diào)用方式(在onCreate()中調(diào)用):
if (NavigationBarUtil.hasNavigationBar(this)) {
NavigationBarUtil.initActivity(findViewById(android.R.id.content));
}
這里我直接使用的系統(tǒng)的布局,首先調(diào)用hasNavigationBar()判斷是否有虛擬按鍵,如果有,則調(diào)用initActivity()初始化NavigationBarUtil工具類,在工具類的構(gòu)造方法中,給傳入的view添加了全局的布局監(jiān)聽器,監(jiān)聽視圖的變化,在監(jiān)聽器中,調(diào)用resetViewHeight1()方法,里面通過CalculateAvailableHeight()獲取虛擬按鍵的高度,根據(jù)橫豎屏的不同,分別設(shè)置了view的高度,實(shí)現(xiàn)了虛擬按鍵布局背景的填充。
總結(jié)
以上所述是小編給大家介紹的Android實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色的變化實(shí)例代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- 修改Android FloatingActionButton的title的文字顏色及背景顏色實(shí)例詳解
- Android實(shí)現(xiàn)沉浸式通知欄通知欄背景顏色跟隨app導(dǎo)航欄背景顏色而改變
- Android設(shè)置PreferenceCategory背景顏色的方法
- Android之scrollview滑動(dòng)使標(biāo)題欄漸變背景色的實(shí)例代碼
- Android 頂部標(biāo)題欄隨滑動(dòng)時(shí)的漸變隱藏和漸變顯示效果
- Android中Fab(FloatingActionButton)實(shí)現(xiàn)上下滑動(dòng)的漸變效果
- Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn)
- Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼
相關(guān)文章
Android Studio 如何刪除/新建一個(gè)module(圖文教程詳解)
這篇文章主要介紹了Android Studio 如何刪除/新建一個(gè)module,此方法也會(huì)將該module從你的硬盤中刪除,如果直接右鍵會(huì)發(fā)現(xiàn)沒有delete選項(xiàng),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2020-03-03
Android自定義scrollview實(shí)現(xiàn)回彈效果
這篇文章主要為大家詳細(xì)介紹了Android自定義scrollview實(shí)現(xiàn)回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android列表實(shí)現(xiàn)單選點(diǎn)擊縮放動(dòng)畫效果
在android開發(fā),我們會(huì)常常使用到縮放動(dòng)畫,這篇文章主要給大家介紹了關(guān)于Android列表實(shí)現(xiàn)單選點(diǎn)擊縮放動(dòng)畫效果的相關(guān)資料,需要的朋友可以參考下2021-08-08
利用Kotlin Tools如何快速添加Kotlin依賴詳解
這篇文章主要給大家介紹了關(guān)于利用Kotlin Tools如何快速添加Kotlin依賴的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Android之ListView分頁加載數(shù)據(jù)功能實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android之ListView分頁加載數(shù)據(jù)功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Flutter版本的自定義短信驗(yàn)證碼實(shí)現(xiàn)示例解析
這篇文章主要介紹了Flutter版本的自定義短信驗(yàn)證碼實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android學(xué)習(xí)教程之動(dòng)態(tài)GridView控件使用(6)
這篇文章主要為大家詳細(xì)介紹了Android動(dòng)態(tài)GridView控件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
android學(xué)習(xí)筆記之View的滑動(dòng)
Android開發(fā)中我們常常需要View滑動(dòng)實(shí)現(xiàn)一些絢麗的效果來優(yōu)化用戶體驗(yàn),下面這篇文章主要給大家介紹了關(guān)于android學(xué)習(xí)筆記之View滑動(dòng)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
Android6.0指紋識(shí)別開發(fā)實(shí)例詳解
這篇文章主要介紹了Android6.0指紋識(shí)別開發(fā)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

