Android實現沉浸式狀態欄

簡介

Android4.4以後,很多APP的狀態欄都不再是黑乎乎的一條,開始出現狀態欄和APP同一個顏色,瞬間感覺高大上了啊,這個是怎麼實現的呢?

基於Android原生代碼實現

需要2步
第一步
新建values-19文件夾,在裏面新增一個style.xml,設置其android:windowTranslucentStatus爲true內容如下:

<resources>
    <style name="AppTheme" parent="@style/BaseAppTheme">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

第二步
在需要顯示的佈局文件中添加兩句話

android:clipToPadding="true"
android:fitsSystemWindows="true"

使用第三方框架SystemBarTint

GIThub上牛人分享的框架SystemBarTint,可以很方便的實現狀態欄透明,省去很多步驟
實現步驟如下
第一步
下載jar包點這兒
並導入項目
第二步
在相應的Activity的onCreate方法中添加如下代碼

//super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);

            SystemBarTintManager mTintManager = new SystemBarTintManager(this);
            mTintManager.setStatusBarTintEnabled(true);
            mTintManager.setNavigationBarTintEnabled(true);
            mTintManager.setTintColor(R.color.colorPrimary);

        }
發佈了71 篇原創文章 · 獲贊 36 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章