開門見山。
原來做的效果,如下圖(頂部有一條明顯的橙色狀態(tài)欄):

改過之后(頂部狀態(tài)欄是透明的):

我發(fā)現(xiàn)網(wǎng)上寫的一些文章,不夠簡潔明了,我整理了一下,復制粘貼一下就可以在項目中運用。
首先,在你的Activity中添加下面四個方法(或者封裝在一個工具類中)
/**
* 全透狀態(tài)欄
*/
protected void setStatusBarFullTransparent() {
if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//虛擬鍵盤也透明
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* 半透明狀態(tài)欄
*/
protected void setHalfTransparent() {
if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
} else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//虛擬鍵盤也透明
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* 如果需要內(nèi)容緊貼著StatusBar
* 應該在對應的xml布局文件中,設(shè)置根布局fitsSystemWindows=true。
*/
private View contentViewGroup;
protected void setFitSystemWindow(boolean fitSystemWindow) {
if (contentViewGroup == null) {
contentViewGroup = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
}
contentViewGroup.setFitsSystemWindows(fitSystemWindow);
}
/**
* 為了兼容4.4的抽屜布局->透明狀態(tài)欄
*/
protected void setDrawerLayoutFitSystemWindow() {
if (Build.VERSION.SDK_INT == 19) {//19表示4.4
int statusBarHeight = getStatusHeight(this);
if (contentViewGroup == null) {
contentViewGroup = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
}
if (contentViewGroup instanceof DrawerLayout) {
DrawerLayout drawerLayout = (DrawerLayout) contentViewGroup;
drawerLayout.setClipToPadding(true);
drawerLayout.setFitsSystemWindows(false);
for (int i = 0; i < drawerLayout.getChildCount(); i++) {
View child = drawerLayout.getChildAt(i);
child.setFitsSystemWindows(false);
child.setPadding(0,statusBarHeight, 0, 0);
}
}
}
}
然后,在Activity的onCreate()方法中調(diào)用即可。示例如下
Activity
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
setHalfTransparent();
setFitSystemWindow(false);
}
protected void setHalfTransparent()...
protected void setStatusBarFullTransparent()...
protected void setFitSystemWindow()...
protected void setDrawerLayoutFitSystemWindow()...
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/drawerLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_start">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="50dp"
android:background="#F86254"
android:text="button"
android:textColor="@color/white" />
</LinearLayout>
1.未做任何設(shè)置
可見,Android5.0以上由于默認是Material Design,頂部是藍色狀態(tài)欄。而5.0以下,默認都是黑色,而且無法修改。

2.半透明狀態(tài)欄,fitsSystemWindows=false
@Override
public void init(Bundle savedInstanceState) {
setHalfTransparent();
setFitSystemWindow(false);
}

可見,5.0以上藍色狀態(tài)欄沒了,變成了半透明的黑色,而內(nèi)容區(qū)域則有了全屏的效果。
但是也要知道一點,那個紅色的TextView,原來是緊貼著狀態(tài)欄,現(xiàn)在是緊貼著屏幕的上邊緣,這樣就導致,內(nèi)容被遮擋。解決這個問題需要一個關(guān)鍵的屬性是setFitSystemWindow=true,追蹤源碼可知,它可以讓我們的布局,paddingTop等于狀態(tài)欄的高度,這樣紅色TextView的位置就會向下移,從而不會被遮擋。
3.半透明狀態(tài)欄,fitsSystemWindows=true
@Override
public void init(Bundle savedInstanceState) {
setHalfTransparent();
setFitSystemWindow(true);
}

此時紅色的TextView,位于狀態(tài)欄下方。
4.全透明狀態(tài)欄,fitsSystemWindows=false
setStatusBarFullTransparent();
setFitSystemWindow(false);

全透明和半透明的區(qū)別在于,狀態(tài)欄是否具有淡黑色的背景,根據(jù)項目需求合理運用。
5.全透明狀態(tài)欄,fitsSystemWindows=true
setStatusBarFullTransparent();
setFitSystemWindow(true);

6.DrawerLayout如何使用
直接使用上述方式,在4.4系統(tǒng)上會出現(xiàn)異常,因此我們需要進行適配。
修改xml文件,DrawerLayout需要添加fitsSystemWindows和clipToPadding屬性,DrawerLayout布局里的一級布局,都需設(shè)置fitsSystemWindows=true。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:id="@+id/drawerLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:clipToPadding="false"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="@drawable/bg_start"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#F86254"
android:text="show"
android:textColor="@color/white" />
</RelativeLayout>
<FrameLayout
android:id="@+id/sideLayout"
android:layout_width="300dp"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@drawable/bg_test">
<Button
android:layout_width="100dp"
android:layout_height="30dp"
android:background="#F86254"
android:text="button"
android:textColor="@color/white" />
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
- 全透明狀態(tài)欄,fitsSystemWindows=false
setStatusBarFullTransparent();

- DrawerLayout全透明狀態(tài)欄,fitsSystemWindows=true
setStatusBarFullTransparent();
setDrawerLayoutFitSystemWindow();

7.可能會錯誤的地方
本來我們有一個界面:

然后按照上面的,添加了代碼之后
setStatusBarFullTransparent();
setFitSystemWindow(true);

然后你提刀來問樓主,這是什么鬼!??!
說好的透明狀態(tài)欄呢,怎么狀態(tài)欄背景色是白色的!

確實是全屏了,狀態(tài)欄也透明了,只是由于,根布局沒設(shè)置背景色,默認的背景色白色,所以你看到的灰色狀態(tài)欄底色,其實是根布局的TopPadding。

8.Activity中嵌套了Fragment,如何使用
另附一張效果圖:

在Activity中設(shè)置setStatusBarFullTransparent(),然后在fragment的xml文件中(這邊寫的粗糙,應該在代碼中,獲取StatusBar高度然后設(shè)置paddingTop):

有興趣可以可以琢磨一下,為什么這么寫,正所謂:學而不思則罔,思而不學則殆。
9.問題
這個方案總體來說其實不是很好,會導致過度繪制,如果對性能要求不是很嚴苛,可以選擇該方案。