標籤:des android style http io color ar 使用 sp
Android 系統自4.2 開始 UI 上就沒多大改變,4.4 也只是增加了透明狀態列與導覽列的功能,
那麼現在我就來給大家講解下如何使用這個新特性,讓你的 app 跟隨潮流,當然如果你不在乎外觀就算了,
使用這個特效能開發出很漂亮的UI,尤其對於 google 原生系統,螢幕下方的導覽列白白佔據一塊螢幕空間,看起來很不爽
OK廢話不多講,開始講技術吧,第一種方法,在代碼設定:
- 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 在,將會很難看,
事實證明,google 並沒有提供一個比較好的解決方案,他的 透明狀態列與導覽列的應用局限於,全屏閱讀文字或玩遊戲那種情景,
第二種方式,是是設定 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 沈浸式透明狀態列與導覽列