stytle方式
用stytle方式設置,需要兼容4.4一下,4.4到5.0,以及5.0以上版本
/values/stytle.xml
<!-- 狀態欄透明設置 必須是4.4以上的版本-->
<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
<!-- 虛擬按鍵透明設置 -->
<item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>
<!-- 5.0以上設置狀態欄的顏色 但是必須是windowTranslucentStatus爲false -->
<!--<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>-->
/values-v19/stytle.xml
<!-- 狀態欄透明設置 必須是4.4以上的版本-->
<item name="android:windowTranslucentStatus">true</item>
<!-- 虛擬按鍵透明設置 -->
<item name="android:windowTranslucentNavigation">true</item>
5.0以上設置狀態欄的顏色 但是必須是windowTranslucentStatus爲false
<item name="android:statusBarColor">@android:color/transparent</item>
/values-v21/stytle.xml
<!--狀態欄透明設置 必須是4.4以上的版本-->
<item name="android:windowTranslucentStatus">false</item>
<!-- 虛擬按鍵透明設置 -->
<item name="android:windowTranslucentNavigation">true</item>
<!-- 5.0以上設置狀態欄的顏色 但是必須是windowTranslucentStatus爲false -->
<item name="android:statusBarColor">@android:color/transparent</item>
代碼裏面設置
同上面的stytle一樣的效果,只是使用的是代碼裏面設置
private void initStatus() {
//版本大於等於4.4
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//獲取到狀態欄設置的兩條屬性
int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
//在4.4之後又有兩種情況 第一種 4.4-5.0 第二種 5.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//第二種 5.0以上
Window window = getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.flags |= flagTranslucentNavigation;
window.setAttributes(attributes);
window.setStatusBarColor(0);
} else {
//第一種 4.4-5.0
Window window = getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
window.setAttributes(attributes);
}
}
}
修改狀態欄顏色
設置狀態欄爲紅色
/**
* 設置StatusBar的顏色
*/
public void setStatusBarColor(){
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
// //給根佈局設置padding值
rootView.setPadding(0,getStatusHeight(),0,0);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//第二種 5.0以上
getWindow().setStatusBarColor(Color.RED);
}else{
//第一種 4.4-5.0
//獲取到根佈局
ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
View statusBar = new View(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusHeight());
statusBar.setBackgroundColor(Color.RED);
statusBar.setLayoutParams(layoutParams);
decorView.addView(statusBar);
}
}
獲取狀態欄高度
/**
* 獲取狀態欄的高度
* @return
*/
public int getStatusHeight(){
//獲取到狀態欄的資源ID
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
//如果獲取到了
if(resourceId>0){
//就返回它的高度
return getResources().getDimensionPixelSize(resourceId);
}
//否則返回0
return 0;
}
獲取到底部虛擬按鍵的高度
/**
* 獲取到底部虛擬按鍵的高度
* @return
*/
public int getNavigationBarHeight(){
//獲取到虛擬按鍵的資源ID
int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
//如果獲取到了
if(resourceId>0){
//就返回它的高度
return getResources().getDimensionPixelSize(resourceId);
}
return 0;
}
處理MD中的statusbar顏色
5.0菜單有陰影:解決辦法給NavigationView 加入app:insetForeground="#00000000"
4.4 可以給最外層佈局設置fitSystemWidows爲true且設置clipToPadding爲false
處理以後