setFeatureInt、android 自定義標題欄


Android 自帶的toolbar 往往不能很好的的滿足我們的個性化要求。因此我們經常使用自定的的標題欄。而Android系統本身也允許我們自定以標題欄。

記錄一下,自定義標題欄常遇到的問題。先上效果圖:

 

實現起來也很簡單。在Activity 的 setContentView方法前添加  

   requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

其含義是允許用戶自定義標題欄。

然后再在setContentView后添加

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_navigation_bar);
其中 R.layout.custom_navigation_bar 為我們自定義的標題欄樣式,
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="44dp"
 6     android:orientation="vertical"
 7     tools:context=".BaseActivity">
 8 
 9     <LinearLayout
10         android:layout_width="match_parent"
11         android:layout_height="match_parent"
12         android:layout_weight="1"
13         android:orientation="horizontal"
14         android:padding="10dp">
15 
16         <LinearLayout
17             android:layout_width="0dp"
18             android:layout_height="match_parent"
19             android:layout_weight="1"
20             android:gravity="left|center_vertical"
21             android:paddingBottom="2dp"
22             android:paddingTop="2dp">
23 
24             <ImageView
25                 android:id="@+id/navigation_left"
26                 android:layout_width="wrap_content"
27                 android:layout_height="match_parent"
28                 android:src="@mipmap/rt_tianjiaguanzhu" />
29 
30             <TextView
31                 android:id="@+id/navigation_left_text"
32                 android:layout_width="wrap_content"
33                 android:layout_height="wrap_content"
34                 android:text="散文"
35                 android:textSize="16sp" />
36 
37         </LinearLayout>
38 
39         <LinearLayout
40             android:layout_width="wrap_content"
41             android:layout_height="match_parent"
42             android:gravity="center"
43             android:paddingBottom="2dp"
44             android:paddingTop="2dp">
45 
46             <TextView
47                 android:id="@+id/navigation_center"
48                 android:layout_width="wrap_content"
49                 android:layout_height="wrap_content"
50                 android:layout_gravity="center"
51                 android:layout_marginRight="5dp"
52                 android:text="全部關注"
53                 android:textAlignment="center"
54                 android:textColor="@color/black"
55                 android:textSize="16sp" />
56 
57             <ImageView
58                 android:layout_width="wrap_content"
59                 android:layout_height="wrap_content"
60                 android:src="@mipmap/icon_down" />
61         </LinearLayout>
62 
63         <LinearLayout
64             android:layout_width="0dp"
65             android:layout_height="match_parent"
66             android:layout_weight="1"
67             android:gravity="right|center_vertical"
68             android:paddingBottom="2dp"
69             android:paddingTop="2dp">
70 
71             <ImageView
72                 android:id="@+id/navigation_right"
73                 android:layout_width="wrap_content"
74                 android:layout_height="match_parent"
75                 android:layout_gravity="center_vertical"
76                 android:src="@mipmap/settings" />
77         </LinearLayout>
78 
79     </LinearLayout>
80 
81     <View
82         android:layout_width="match_parent"
83         android:layout_height="0.5dp"
84         android:background="@color/lightgray" />
85 
86 </LinearLayout>
View Code

最后設置主題,也只最容易出錯的地方。對使用該方式添加標題欄的Activity 添加主題樣式。

這是我的主題樣式 應繼承  android:Theme.Light 主題否則會報錯。

1     <!-- 標題欄樣式 -->
2     <style name="WindowTitleBackground" >
3         <item name="android:background">@color/colordarkgray</item>
4     </style>
5     <style name="MyTheme" parent="android:Theme.Light">
6         <item name="android:windowTitleSize">45dp</item>
7         <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
8     </style>
View Code

Activity 引用代碼如下。

android:theme="@style/MyTheme" 

這樣就可以實現自定義的效果了。


補充上面說主題不繼承 android:Theme.Light 會報錯。查了一下是標題欄沖突的原因。
    我們只需要在繼承的主題里添加如下屬性就可以了。
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">false</item>
這樣我們就可以繼承別的主題使用一些高大上的效果了。如 Theme.AppCompat主題等。

有朋友問我,顯示這個標題的時候同時也顯示了系統自帶的標題是什么原因,查看代碼后發現他的Activity繼承了AppCompatActivity 這里我們應繼承Activity 就好了。






 
        

 


免責聲明!

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



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