Translucent Bar Android狀態欄自定義顏色


 

Android4.4 一個很重要的改變就是透明系統欄.。新的系統欄是漸變透明的, 可以最大限度的允許屏幕顯示更多內容, 也可以讓系統欄和 Action Bar 融為一體, 僅僅留下最低限度的背景保護以免通知通知欄內容和 Action Bar 文字/圖標難以識別。谷歌把這種效果稱之為:Translucent Bar。

它的初始目的就是要最大化可視面積和淡化系統界面的存在感。其效果如圖:

實現方法也很簡單(僅限4.4以上的系統)

  1、新建主題我這里分成了3個主題(4.4之前用的主題、4.4~5.0用的主題、5.0之后用的主題 <5.0之后必須指定顏色>)

styles(4.0之前)

1  <!-- 半透明的主題樣式 -->
2     <style name="NavigationTransparent" parent="AppTheme">
3         <!-- Android 4.4 之前的版本跟隨系統默認的樣式-->
4     </style>
View Code

styles(4.4之后)

1  <!-- 半透明的主題樣式 -->
2     <style name="NavigationTransparent" parent="AppTheme">
3         <!-- 狀態欄 半透明 -->
4         <item name="android:windowTranslucentStatus">true</item>
5         <!-- 導航欄 半透明-->
6         <item name="android:windowTranslucentNavigation">true</item>
7     </style>
View Code

styles(5.0之后)

1  <!-- 半透明的主題樣式 -->
2     <style name="NavigationTransparent" parent="AppTheme">
3         <!-- 導航欄 半透明-->
4         <item name="android:windowTranslucentNavigation">true</item>
5         <!-- 狀態欄 半透明 -->
6         <item name="android:windowTranslucentStatus">true</item>
7         <!-- Android 5.0開始需要把顏色設置為透明。否狀態欄會用系統默認的淺灰色 (Android 5.0以后支持修改顏色,需指定顏色)-->
8         <item name="android:statusBarColor">@android:color/transparent</item>
9     </style>
View Code

  2、布局文件里添加

android:fitsSystemWindows="true"
不設置這個屬性的話,布局文件的內容會跑到狀態欄中。
 
 3、AndroidManifast.xml為該Activity 指定該主題樣式。



上面的我們稱之為 “透明狀態欄”


====================================================================================================================================
============================================= 猶豫的分隔線=====================================================
====================================================================================================================================


還有一種方式是大家比較常見的,狀態欄的顏色和導航菜單的顏色一致 如圖:


 限5.0以上版本

  1、我們在styles(5.0)中添加如下主題

1  <style name="NavigationChange" parent="AppTheme">
2         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
3     </style>
View Code

  同時也應該在styles 里面添加相同主題,防止手機版本不夠,主題報錯。內容我們為空,調用系統默認顏色

styles

1     <!-- 可以改變的主題樣式 -->
2     <style name="NavigationChange" parent="AppTheme">
3 
4     </style>
View Code

  2、我們自定義導航欄菜單

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     android:fitsSystemWindows="true"
 8     >
 9 
10     <RelativeLayout
11         android:layout_width="match_parent"
12         android:layout_height="55dp"
13         android:background="@color/colorPrimaryDark">
14         <TextView
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"
17             android:layout_centerInParent="true"
18             android:text="這是導航欄標題啊"
19             android:textSize="20sp"/>
20 
21     </RelativeLayout>
22 
23     <Button
24         android:id="@+id/jump"
25         android:layout_margin="20dp"
26         android:text="點擊跳轉到另一個Activity"
27         android:layout_width="match_parent"
28         android:layout_height="wrap_content"
29         android:onClick="jump"
30         />
31 
32 </LinearLayout>
View Code

  3、AndroidManifast.xml 文件中為Activity 指定該主題樣式就好了。

 

補充:

  • 兩種方式其實都是改變狀態欄的顏色,只是方式一的顏色為透明色。  
  • 方式一適用於app中沒有導航欄,且整體的背景是一張圖片的界面;
  • 方式二適用於app中導航欄顏色為純色的界面;


看到一張圖很詳細,粘貼一下。







 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM