android透明背景代碼:android狀態欄透明效果

前言

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

「安卓乾貨鋪」-實現Android透明狀態欄的總結

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

「安卓乾貨鋪」-實現Android透明狀態欄的總結

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

首先,在你的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以下,默認都是黑色,而且無法修改。

「安卓乾貨鋪」-實現Android透明狀態欄的總結

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

@Overridepublic void init(Bundle savedInstanceState) { setHalfTransparent(); setFitSystemWindow(false);
}
「安卓乾貨鋪」-實現Android透明狀態欄的總結

可見,5.0以上藍色狀態欄沒了,變成了半透明的黑色,而內容區域則有了全屏的效果。

但是也要知道一點,那個紅色的TextView,原來是緊貼著狀態欄,現在是緊貼著屏幕的上邊緣,這樣就導致,內容被遮擋。解決這個問題需要一個關鍵的屬性是setFitSystemWindow=true,追蹤源碼可知,它可以讓我們的布局,paddingTop等於狀態欄的高度,這樣紅色TextView的位置就會向下移,從而不會被遮擋。

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

@Overridepublic void init(Bundle savedInstanceState) { setHalfTransparent(); setFitSystemWindow(true);
}
「安卓乾貨鋪」-實現Android透明狀態欄的總結

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

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

setStatusBarFullTransparent(); 
setFitSystemWindow(false);
「安卓乾貨鋪」-實現Android透明狀態欄的總結

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

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

setStatusBarFullTransparent();
setFitSystemWindow(true);
「安卓乾貨鋪」-實現Android透明狀態欄的總結

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();
「安卓乾貨鋪」-實現Android透明狀態欄的總結

(2).DrawerLayout全透明狀態欄,fitsSystemWindows=true

setStatusBarFullTransparent();
setDrawerLayoutFitSystemWindow();
「安卓乾貨鋪」-實現Android透明狀態欄的總結

7.可能會錯誤的地方

本來我們有一個界面:

「安卓乾貨鋪」-實現Android透明狀態欄的總結

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

setStatusBarFullTransparent();
setFitSystemWindow(true);
「安卓乾貨鋪」-實現Android透明狀態欄的總結

然後你提刀來問樓主,這是什麼鬼!!!

說好的透明狀態欄呢,怎麼狀態欄背景色是白色的!

「安卓乾貨鋪」-實現Android透明狀態欄的總結

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

「安卓乾貨鋪」-實現Android透明狀態欄的總結

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

另附一張效果圖:

「安卓乾貨鋪」-實現Android透明狀態欄的總結

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

「安卓乾貨鋪」-實現Android透明狀態欄的總結

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/221926.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 13:25
下一篇 2024-12-09 13:25

相關推薦

發表回復

登錄後才能評論