Android實現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色的變化實例代碼詳解
今天介紹一下,我在項目開發(fā)過程中,實現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色變化的方法,實現(xiàn)方式是,通過隱藏系統(tǒng)的狀態(tài)欄和虛擬按鍵的背景,實現(xiàn)圖片和背景顯示到狀態(tài)欄和虛擬按鍵下方。下面來看實現(xiàn)代碼:
實現(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);
}
/**
* 使狀態(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è)置虛擬按鍵背景透明,同時該屬性會實現(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();//請求重新布局
}
/**
* 獲取狀態(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為分界,分別實現(xiàn)狀態(tài)欄背景的透明,然后是調(diào)用setStatusBarTextColor()方法,設(shè)置狀態(tài)欄字體的顏色。
實現(xiàn)效果:
1、沉浸式

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

如果要填充自己需要的導(dǎo)航欄顏色的話,可以自己創(chuàng)建一個導(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的實際高度
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();//請求重新布局
usableHeightView = usableHeightViewNow;
}
}
/**
* 計算試圖高度
*
* @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的高度,實現(xiàn)了虛擬按鍵布局背景的填充。
總結(jié)
以上所述是小編給大家介紹的Android實現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色的變化實例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- 修改Android FloatingActionButton的title的文字顏色及背景顏色實例詳解
- Android實現(xiàn)沉浸式通知欄通知欄背景顏色跟隨app導(dǎo)航欄背景顏色而改變
- Android設(shè)置PreferenceCategory背景顏色的方法
- Android之scrollview滑動使標題欄漸變背景色的實例代碼
- Android 頂部標題欄隨滑動時的漸變隱藏和漸變顯示效果
- Android中Fab(FloatingActionButton)實現(xiàn)上下滑動的漸變效果
- Android中Toolbar隨著ScrollView滑動透明度漸變效果實現(xiàn)
- Android直播軟件搭建之實現(xiàn)背景顏色滑動漸變效果的詳細代碼
相關(guān)文章
Android 出現(xiàn)“Can''t bind to local 8602 for debugger”錯誤的解決方法
這篇文章主要介紹了Android 出現(xiàn)“Can't bind to local 8602 for debugger”錯誤的解決方法的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android內(nèi)存溢出及內(nèi)存泄漏原因進解析
這篇文章主要介紹了Android內(nèi)存溢出及內(nèi)存泄漏原因解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
Android自定義ListView實現(xiàn)下拉刷新上拉加載更多
Listview現(xiàn)在用的很少了,基本都是使用Recycleview,但是不得不說Listview具有劃時代的意義,我們可以自己添加下拉刷新,上拉加載更多功能。本文就來利用自定義ListView實現(xiàn)下拉刷新上拉加載更多效果,需要的可以參考一下2022-10-10
Android shell命令行中過濾adb logcat輸出的幾種方法
本文主要介紹Android shell命令行中過濾adb logcat輸出的方法,這里整理了幾種方法,并詳細的說明,有需要的朋友可以參考下2016-08-08
android編程判斷應(yīng)用是否具有某個權(quán)限的方法
這篇文章主要介紹了android編程判斷應(yīng)用是否具有某個權(quán)限的方法,涉及Android進程操作及權(quán)限控制的相關(guān)使用技巧,需要的朋友可以參考下2015-10-10
Android 獲取系統(tǒng)語言的實例(兼容7.0)
下面小編就為大家?guī)硪黄狝ndroid 獲取系統(tǒng)語言的實例(兼容7.0)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
Flutter基于Dart Unwrapping Multiple Optional小技巧
這篇文章主要為大家介紹了Flutter Unwrapping Multiple Optional打開多個選項小技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12

