設定 狀态欄透明,隻支援4.4及以上版本。
/**
* 設定狀态欄顔色
*/
public static void setStatusBarColor(Activity activity) {
try {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// activity.getWindow().setStatusBarColor(Color.argb(0,0,0,0));
// }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 生成一個狀态欄大小的矩形
View statusBarView = new View(activity);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getStatusBarHeight(activity));
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(Color.argb(0,0,0,0));
// 添加 statusView 到布局中
ViewGroup decorView = (ViewGroup)activity.getWindow().getDecorView();
decorView.addView(statusBarView);
}
} catch (Exception e) {
e.printStackTrace();
LogUtil.e("zhouhao","設定狀态欄透明異常,方法名:setStatusBarColor");
}
}
/**
* 擷取手機狀态欄高度
* @param activity
* @return
*/
public static int getStatusBarHeight(Activity activity){
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
int statusHeight = activity.getResources().getDimensionPixelSize(resourceId);
return statusHeight;
}
具體調用:在Activity的onCreate方法裡調用setStatusBarColor即可。
提示:注釋的地方打開之後6.0的手機透明不了。