關於如何實現Android透明狀態欄的總結


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


 
a1.gif

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


 
p2.gif

我發現網上寫的一些文章,不夠簡潔明了,我整理了一下,復制粘貼一下就可以在項目中運用。

首先,在你的Activity中添加下面四個方法(或者封裝在一個工具類中)

   /** * 全透狀態欄 */ 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); } } /** * 半透明狀態欄 */ 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); } } /** * 如果需要內容緊貼着StatusBar * 應該在對應的xml布局文件中,設置根布局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的抽屜布局->透明狀態欄 */ 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()方法中調用即可。示例如下

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.未做任何設置

可見,Android5.0以上由於默認是Material Design,頂部是藍色狀態欄。而5.0以下,默認都是黑色,而且無法修改。


 
nothing.png

2.半透明狀態欄,fitsSystemWindows=false

@Override public void init(Bundle savedInstanceState) { setHalfTransparent(); setFitSystemWindow(false); } 

 

 
half_not_fit.png

可見,5.0以上藍色狀態欄沒了,變成了半透明的黑色,而內容區域則有了全屏的效果。
但是也要知道一點,那個紅色的TextView,原來是緊貼着狀態欄,現在是緊貼着屏幕的上邊緣,這樣就導致,內容被遮擋。解決這個問題需要一個關鍵的屬性是 setFitSystemWindow=true,追蹤源碼可知,它可以讓我們的布局, paddingTop等於狀態欄的高度,這樣紅色TextView的位置就會向下移,從而不會被遮擋。

 

3.半透明狀態欄,fitsSystemWindows=true

@Override public void init(Bundle savedInstanceState) { setHalfTransparent(); setFitSystemWindow(true); } 
 
half_fit.png

此時紅色的TextView,位於狀態欄下方。

4.全透明狀態欄,fitsSystemWindows=false

setStatusBarFullTransparent();
setFitSystemWindow(false); 
 
full_no_fit.png

全透明和半透明的區別在於,狀態欄是否具有淡黑色的背景,根據項目需求合理運用。

5.全透明狀態欄,fitsSystemWindows=true

setStatusBarFullTransparent();
setFitSystemWindow(true); 
 
full_fit.png

6.DrawerLayout如何使用

直接使用上述方式,在4.4系統上會出現異常,因此我們需要進行適配。
修改xml文件,DrawerLayout需要添加fitsSystemWindows和clipToPadding屬性,DrawerLayout布局里的一級布局,都需設置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> 
  1. 全透明狀態欄,fitsSystemWindows=false
setStatusBarFullTransparent();
 
drawerlayout_nofit.png
  1. DrawerLayout全透明狀態欄,fitsSystemWindows=true
setStatusBarFullTransparent();
setDrawerLayoutFitSystemWindow();
 
drawerlayout.png

7.可能會錯誤的地方

本來我們有一個界面:


 
 

然后按照上面的,添加了代碼之后

setStatusBarFullTransparent();
setFitSystemWindow(true); 
 
image.png

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


 
 

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


 
 

 

8.Activity中嵌套了Fragment,如何使用

另附一張效果圖:


 
 

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

 
 

有興趣可以可以琢磨一下,為什么這么寫,正所謂:學而不思則罔,思而不學則殆。

 

9.問題

這個方案總體來說其實不是很好,會導致過度繪制,如果對性能要求不是很嚴苛,可以選擇該方案。



作者:桂林的小河
鏈接:https://www.jianshu.com/p/e89ee0a77bb5
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM