自定義狀態(tài)欄的實現(xiàn)一般分為兩種方式:
1)將布局內容延展至狀態(tài)欄;
2)使用沉浸式狀態(tài)欄斟珊。
由于大部分android系統(tǒng)狀態(tài)欄采用黑色背景浮毯,白色的字體圖標。這導致以上兩種實現(xiàn)方式存在一個共性問題舀瓢,即狀態(tài)欄背景顏色定義為白色等淺色調時,這導致狀態(tài)欄顯示白屏耗美。
該問題只能通過更改狀態(tài)欄字體圖標的顏色來解決京髓,但存在兼容性問題。目前只有MIUI6及以上商架、魅族以及android 6.0以上可以設置堰怨。
注意,測試手機MX5蛇摸,android系統(tǒng)5.1备图,flyme6.0.2.0A,在設置狀態(tài)欄顏色setStatusBar后皇型,系統(tǒng)會根據(jù)statusbar的色調自動變更狀態(tài)欄字體圖標顏色诬烹。
一、布局內容延展至狀態(tài)欄
注意:android 4.4及以上可以實現(xiàn)該功能弃鸦。
實現(xiàn)該功能最簡單粗暴的方法是使用屬性windowTranslucentStatus="true"绞吁,該屬性使布局內容得以延伸到狀態(tài)欄和導航欄所在的空間,形成類似全屏的視覺效果唬格。但使用該方法后會引入一些問題家破。
該屬性的java設置方法為:
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
1、界面布局上部分和狀態(tài)欄重合购岗。
該問題的解決方案:
1) 界面根布局設置屬性fitsSystemWindow=true汰聋,該屬性使得屏幕上的可布局空間位于狀態(tài)欄下方與導航欄上方。但該屬性只對簡單布局有效喊积,且可能引入EditText輸入時軟鍵盤遮擋光標的問題烹困;
一個高票workround解決方案如下:
//調用方式
//To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
//實現(xiàn)方法
public class AndroidBug5497Workaround {
// For more information, see https://code.google.com/p/android/issues/detail?id=5497
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
public static void assistActivity (Activity activity) {
new AndroidBug5497Workaround(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(Activity activity) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4)) {
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
}
2)手動在布局中添加StatusBar的空間,設置一個25dp的view來搞定乾吻。注意髓梅,可能不同版本的StatusBar高度不同拟蜻,需要用dimen來設備
3)代碼設置根布局的margin;topMagin的值等于狀態(tài)欄的高度枯饿。缺點是每個界面布局需要使用統(tǒng)一的布局格式
//獲取狀態(tài)欄高度
public static int getStatusBarHeight(Context context)
{
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
{
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
該方案在面對淺色狀態(tài)布局內容時酝锅,存在限制。
參考:http://blog.csdn.net/ling9400/article/details/59478358
https://www.zhihu.com/question/31468556
二奢方、沉浸式狀態(tài)欄
如果不需要支持 4.4搔扁,建議使用 statusBarColor
如果需要支持 4.4,建議 4.4 使用 windowTranslucentStatus蟋字;5.x 使用 statusBarColor/colorPrimaryDark
方案如下
//設置狀態(tài)欄顏色
1稿蹲、activity.getWindow().setStatusBarColor(int color)
//因為 FLAT_LAYOUT_STABLE 和 FLAG_LAYOUT_FULLSCREEN 是伴隨 translucentStatus 自動設置的,v21 上需要手動設置
2鹊奖、activity.getWindow()
.getDecorView()
.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
//調整view的高度场绿,可以動態(tài)設置為StatusBar 的 Height;或者設置padding
3嫉入、<android.support.v7.widget.Toolbar
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:elevation="@dimen/space_small"
android:paddingTop="@dimen/status_bar_height/>
//dimens.xml
<dimen name="status_bar_height">25dp</dimen>
//values-v23/dimens.xml
<dimen name="status_bar_height">24dp</dimen>
//小米沉浸式狀態(tài)欄
https://dev.mi.com/doc/p=4769/ //
//魅族沉浸式狀態(tài)欄
http://open-wiki.flyme.cn/index.php?title=%E7%8A%B6%E6%80%81%E6%A0%8F%E5%8F%98%E8%89%B2
三、深色狀態(tài)欄方案
注意璧尸,改變狀態(tài)欄文字圖標顏色的代碼咒林,需要在主線程運行。
關于機型判斷爷光,可參考一下文章:
//魅族機型判斷
http://blog.csdn.net/sslinp/article/details/50535188
////如何判斷小米系統(tǒng)
https://dev.mi.com/doc/?p=254
一垫竞、魅族手機設置
//注意該方法需要在setStatusColor調用前設置,否則系統(tǒng)狀態(tài)欄顏色變?yōu)楹谏?public static boolean setMeiZuStatusBarDarkIcon(Window window, boolean dark) {
boolean result = false;
if (window != null) {
try {
WindowManager.LayoutParams lp = window.getAttributes();
Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
darkFlag.setAccessible(true);
meizuFlags.setAccessible(true);
int bit = darkFlag.getInt(null);
int value = meizuFlags.getInt(lp);
if (dark) {
value |= bit;
} else {
value &= ~bit;
}
meizuFlags.setInt(lp, value);
window.setAttributes(lp);
result = true;
} catch (Exception e) {
Log.e("MeiZu", "setStatusBarDarkIcon: failed");
}
}
return result;
}
二蛀序、小米手機方案
//MIUI 6.0以上
public static boolean setMIUIStatusBarLightMode(Window window, boolean dark) {
boolean result = false;
if (window != null) {
Class clazz = window.getClass();
try {
int darkModeFlag = 0;
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
if(dark){
extraFlagField.invoke(window,darkModeFlag,darkModeFlag);//狀態(tài)欄透明且黑色字體
}else{
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字體
}
result=true;
}catch (Exception e){
}
//在新的 MIUI 版本(即基于 Android 6.0 欢瞪,開發(fā)版 7.7.13 及以后版本).使用新方案
//參考http://www.miui.com/thread-8946673-1-1.html
//https://dev.mi.com/console/doc/detail?pId=1159
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (dark) {
int flag = window.getDecorView().getSystemUiVisibility() |
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
window.getDecorView().setSystemUiVisibility(flag);
} else {
int flag = window.getDecorView().getSystemUiVisibility() &
~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
window.getDecorView().setSystemUiVisibility(flag);
}
}
}
return result;
}
三、android M(6.0)及以上方案
//compileSdkVersion為6.0及以上
public boolean setStatusBarLightMode(Activity activity, boolean isFontColorDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (isFontColorDark) {
// 沉浸式
// activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//非沉浸式
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
//非沉浸式
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
return true;
}
return false;
}
參考: 小米官網(wǎng)解決方案MIUI6+
其他:
http://www.voidcn.com/blog/wds1181977/article/p-6167721.html
http://blog.csdn.net/jo__yang/article/details/51456126