android設置透明狀態欄


先是半透明效果(兩種方法):

第一種(簡單):

//直接將下面的代碼放在activity中的setContentView(R.layout.activity_main);中之前就行了

if (Build.VERSION.SDK_INT >= 21) {
    View decorView = getWindow().getDecorView();                   
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    getWindow().setStatusBarColor(Color.TRANSPARENT);
}

  

第二種(復雜)

1.先修改 res / values / 目錄下的styles.xml文件

<resources>
 
    <stylename="AppTheme"parent="@style/BaseTheme">
 
    </style>
    <stylename="BaseTheme"parent="Theme.AppCompat.Light.NoActionBar">
        <itemname="colorPrimary">@color/colorPrimary</item>
        <itemname="colorPrimaryDark">@color/colorPrimaryDark</item>
    </style>
 
</resources>

  


2.然后我們在res下新建一個values-v19的目錄(代表最低API為19),再在其中新建一個styles.xml
<resources>
 
    <stylename="AppTheme"parent="@style/BaseTheme">
        <itemname="android:windowTranslucentNavigation">true</item>
        <itemname="android:windowTranslucentStatus">true</item>
    </style>
 
</resources>

  


3.設置MainActivity的布局文件為
<FrameLayout 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:fitsSystemWindows="true"//這句代碼是控制不讓toolbar和狀態欄重疊,大家可以刪了試一下,這里我就不上圖了
    android:background="#fff000">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"

        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>//這兩句代碼用於切換色系不然是黑色的字體和深色系的彈出框(不用去理解)


</FrameLayout>

  


*為了看的出來是透明的,所以把背景顏色設置為了 #fff000

 

4.在MainActivity中加入

 

Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);

  

OK總共就4步,比網上很多教程都簡單吧

 

以下為效果圖:

雖然很丑但是還是能看出來狀態欄已經是半透明的狀態了就透出一些背景顏色,大致上就統一了色系

 

全透明效果:

直接在MainActivity中setContentView(R.layout.activity_main)之前加上以下代碼

getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }

  

完成后,效果圖如下

      


免責聲明!

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



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