一.5.0及以上實現沉浸式狀態欄
方式1:通過設置主題達到,狀態欄的顏色跟隨你的主題里面的colorPrimaryDark屬性。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
方式2:通過代碼實現(只對API 21 及以上才能通過代碼實現),狀態欄顯示為紅色
getWindow().setStatusBarColor(Color.RED);
方式3:通過在主題中設置樣式屬性解決,
<item name="android:statusBarColor">@color/color_red</item>
二.4.4 小于android 5.0實現沉浸式狀態欄(小于4.4就不能實現),可以設置狀態欄為透明的
方式1:通過在主題中設置樣式屬性解決,(不推薦使用,兼容性不好)
<item name="android:windowTranslucentStatus">true</item>
方式2:在代碼里面解決,設置為透明的屬性(在布局文件中加入Toolbar)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setContentView(R.layout.activity_main);
出現副作用: APP的內容頂到最上面去了,即狀態欄會遮擋一部分界 面。很坑 解決辦法(有幾種): 1)給Toolbar設置android:fitsSystemWindows="true" 該屬性的作用:設置布局時,是否考慮當前系統窗口的布局,如果為true就會調整整個系統窗口 布局(包括狀態欄的view)以適應你的布局。但是又出現了一個bug,當里面有ScrollView并且ScrollView里面有Edittext的時候,就會出現軟鍵盤一彈起就會把toolbar拉下來,很難看這種辦法有什么價值呢?如果里面沒有ScrollView就可以用。
解決辦法:給布局最外層容器設置android:fitsSystemWindows="true" 可以達到狀態欄透明,并且露出底色---android:windowBackground顏色。
巧妙地解決:步驟:
1.在最外層容器設置android:fitsSystemWindows="true"
2.直接將最外層容器(也可以修改-android:windowBackground顏色)設置成狀態欄想要的顏色
3.下面剩下的布局再包裹一層正常的背景顏色
方式3:在布局文件中添加Toobar,修改Toolbar的高度,步驟:
1.不要給Toolbar設置android:fitsSystemWindows="true"
2.獲取狀態欄的高度.
源碼:
<!-- Height of the status bar -->
<dimen name="status_bar_height">24dp</dimen>
<!-- Height of the bottom navigation / system bar. -->
<dimen name="navigation_bar_height">48dp</dimen>
反射手機運行的類(R類):android.R.dimen.status_bar_height.
3.在布局文件中添加Toobar,給Toolbar設置padding值
代碼:
public class MainActivity extends AppCompatActivity {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.WHITE);
ViewGroup.LayoutParams params = toolbar.getLayoutParams();
params.height += getStatusBarHeight(this);
toolbar.setLayoutParams(params);
toolbar.setPadding(toolbar.getPaddingLeft(),
toolbar.getPaddingTop()+getStatusBarHeight(this),
toolbar.getPaddingRight(),
toolbar.getPaddingBottom()
);
}
private int getStatusBarHeight(Context context) {
int statusBarHeight = -1;
//反射手機運行的類(R類):android.R.dimen.status_bar_height.
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object newInstance = clazz.newInstance();
String statusBarHeightStr = clazz.getField("status_bar_height").get(newInstance).toString();
//獲得status_bar_height的id
int id = Integer.parseInt(statusBarHeightStr);
//將DP變成px
statusBarHeight = context.getResources().getDimensionPixelSize(id);
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:title="網易新聞"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="?actionBarSize">
</android.support.v7.widget.Toolbar>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</LinearLayout>
BaseActivity的封裝:
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT <Build.VERSION_CODES.LOLLIPOP){
//在[4.4 , 5.0)設置狀態欄為透明
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
super.onCreate(savedInstanceState);
}
public void setOrChangeTransluentBar(Toolbar toolbar,int statusBarColor){
toolbar.setBackgroundColor(statusBarColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT <Build.VERSION_CODES.LOLLIPOP){
//在[4.4 , 5.0)設置狀態欄為透明
ViewGroup.LayoutParams params = toolbar.getLayoutParams();
params.height += getStatusBarHeight(this);
toolbar.setLayoutParams(params);
toolbar.setPadding(toolbar.getPaddingLeft(),
toolbar.getPaddingTop()+getStatusBarHeight(this),
toolbar.getPaddingRight(),
toolbar.getPaddingBottom()
);
}
}
private int getStatusBarHeight(Context context) {
int statusBarHeight = -1;
//反射手機運行的類(R類):android.R.dimen.status_bar_height.
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object newInstance = clazz.newInstance();
String statusBarHeightStr = clazz.getField("status_bar_height").get(newInstance).toString();
//獲得status_bar_height的id
int id = Integer.parseInt(statusBarHeightStr);
//將DP變成px
statusBarHeight = context.getResources().getDimensionPixelSize(id);
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}
}
主題:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
在4.4上運行效果圖:
在6.0上運行效果圖