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

來源:互聯網
上載者:User

標籤:des   android   style   http   io   color   ar   使用   sp   

Android 系統自4.2 開始 UI 上就沒多大改變,4.4 也只是增加了透明狀態列與導覽列的功能,




那麼現在我就來給大家講解下如何使用這個新特性,讓你的 app 跟隨潮流,當然如果你不在乎外觀就算了,
使用這個特效能開發出很漂亮的UI,尤其對於 google 原生系統,螢幕下方的導覽列白白佔據一塊螢幕空間,看起來很不爽



OK廢話不多講,開始講技術吧,第一種方法,在代碼設定:

  1. if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
  2.                                 //透明狀態列
  3.                                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  4.                                 //透明導覽列
  5.                                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  6.                         }
直接調用上面2行代碼可以透明,但是你會發現你的 view 跑到 actionbar 上面去了,很明顯 google 的意圖是使你的 view 可以佔據整個螢幕,然後 狀態列和導覽列 透明覆蓋在上面很明顯這樣不可行。
那有沒有辦法使你的 view 保持原來大小呢?
有,你需要在這個 activity 的 layout xml 檔案添加兩個屬性
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:layout_width="fill_parent"
  3.     android:layout_height="fill_parent"
  4.     android:gravity="center_horizontal"
  5.     
  6.     android:fitsSystemWindows="true"
  7.     android:clipToPadding="true"
  8.     
  9.     android:orientation="vertical" >
這樣狀態列的背景就是你的 activity 的主背景,倘若actionbar 在,將會很難看,

事實證明,google 並沒有提供一個比較好的解決方案,他的 透明狀態列與導覽列的應用局限於,全屏閱讀文字或玩遊戲那種情景,


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

  1. android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.TranslucentDecor"
  2.             android:theme="@android:style/Theme.Holo.Light.NoActionBar.TranslucentDecor"
  3.             android:theme="@android:style/Theme.Holo.NoActionBar.TranslucentDecor"
複製代碼如果你使用自定主題,只需在在 values-19 檔案添加以下屬性:
  1. <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">

  2.         <!-- API 19 theme customizations can go here. -->
  3.         <item name="android:windowTranslucentStatus">true</item>
  4.         <item name="android:windowTranslucentNavigation">true</item>
  5.     </style>
複製代碼
剛剛說了這個使用有局限性,不過好在有一個開源的東西
https://github.com/jgilfelt/SystemBarTint


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

使用這個主題

  1. <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">

  2.         <!-- API 19 theme customizations can go here. -->
  3.         <item name="android:windowTranslucentStatus">true</item>
  4.         <item name="android:windowTranslucentNavigation">true</item>
  5.     </style>

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


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




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.