Android 4.4 沈浸式透明狀態列與導覽列

來源:互聯網
上載者:User

標籤:art   win   sch   --   調用   param   dev   get   安卓   

安卓4.4才有的沈浸式狀態列

在代碼設定:

if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {    //透明狀態列   getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);   //透明導覽列   getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);}

直接調用上面2行代碼可以透明,但是你會發現你的 view 跑到 actionbar 上面去了,很明顯

google 的意圖是使你的 view 可以佔據整個螢幕,然後 狀態列和導覽列 透明覆蓋在上面很明顯這樣不可行。
那有沒有辦法使你的 view 保持原來大小呢?
有,你需要在這個 activity 的 layout xml 檔案添加兩個屬性:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:gravity="center_horizontal"        android:fitsSystemWindows="true"    android:clipToPadding="true"        android:orientation="vertical" >

這樣狀態列的背景就是你的 activity 的主背景,倘若actionbar 在,將會很難看,

第二種方式,是是設定 theme 屬性

android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.TranslucentDecor"android:theme="@android:style/Theme.Holo.Light.NoActionBar.TranslucentDecor"android:theme="@android:style/Theme.Holo.NoActionBar.TranslucentDecor"

複製代碼 如果你使用自定主題,只需在在 values-19 檔案添加以下屬性:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">        <!-- API 19 theme customizations can go here. -->        <item name="android:windowTranslucentStatus">true</item>        <item name="android:windowTranslucentNavigation">true</item></style>

複製代碼 

剛剛說了這個使用有局限性,不過好在有一個開源的東西
https://github.com/jgilfelt/SystemBarTint


使用這個開源庫,必須開啟透明標題列

 

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">        <!-- API 19 theme customizations can go here. -->        <item name="android:windowTranslucentStatus">true</item>        <item name="android:windowTranslucentNavigation">true</item></style>

使用這個主題或者在setContentView之前調用這個代碼

 if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {     //透明狀態列     getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);     //透明導覽列     getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);}

 

Android 4.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.