實現android4.4沈浸式標題列

來源:互聯網
上載者:User

標籤:

查閱各大網站,最後結合自己的時間,總結出了兩種可行的方法

一.修改樣式檔案:

1.增加values-v19檔案夾,再其中的styles.xml中加上:

<!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">        <!-- toolbar(actionbar)顏色 -->        <item name="colorPrimary">@color/colorPrimary</item>        <!-- 狀態列顏色 -->        <item name="colorPrimaryDark">@color/colorPrimary</item>        <!-- toolbar標題顏色 -->        <item name="android:textColorPrimary">@color/white</item>        <!-- 視窗的背景顏色 -->        <item name="android:windowBackground">@color/colorPrimary</item>        <!-- translucent system bars-->        <item name="android:windowTranslucentStatus" >true</item>        <item name="android:windowTranslucentNavigation">true</item>    </style>

二.使用開源庫

1.android studio中加入依賴:

compile ‘com.readystatesoftware.systembartint:systembartint:1.0.3‘

2.在activity的onCreate()中調用下面方法:

   /**     * Apply KitKat specific translucency.     */    private void applyKitKatTranslucency() {        // KitKat translucent navigation/status bar.        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            setTranslucentStatus(true);            SystemBarTintManager mTintManager = new SystemBarTintManager(this);            mTintManager.setStatusBarTintEnabled(true);            mTintManager.setNavigationBarTintEnabled(true);            // mTintManager.setTintColor(0xF00099CC);            mTintManager.setTintDrawable(UIElementsHelper                    .getGeneralActionBarBackground(this));        }    }    @TargetApi(19)    private void setTranslucentStatus(boolean on) {        Window win = getWindow();        WindowManager.LayoutParams winParams = win.getAttributes();        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;        if (on) {            winParams.flags |= bits;        } else {            winParams.flags &= ~bits;        }        win.setAttributes(winParams);    }

使用以上兩個方法的最後,都要在主布局中加上:

android:fitsSystemWindows="true"

否則標題列是白色的。

實現android4.4沈浸式標題列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.