原裝的Android標(biāo)題欄配色比較單調(diào),就是黑色的一坨,現(xiàn)在假設(shè)你的軟件需要獨自添加標(biāo)題欄,這樣不僅美觀而且可以將進度條等加進去,如何實現(xiàn):
方法一、在你的那張Activity中onCreate方法中加上下面代碼:
1
2
3
|
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); //軟件activity的布局 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); //titlebar為自己標(biāo)題欄的布局 |
但是新的問題又來了,這樣是無法深層的定制標(biāo)題欄的,比如原有的高度和背景都沒有發(fā)生變化,那有沒有好的方法呢?答案是有的、
方法二:
因此先定義一個style,若修改背景請修改android:windowTitleBackgroundStyle
若修改標(biāo)題欄高度,請修改android:windowTitleSize
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "utf-8" ?> < resources xmlns:android = "http://schemas.android.com/apk/res/android" > < style name = "CustomWindowTitleBackground" > < item name = "android:background" >#565656</ item > </ style > < style name = "test" parent = "android:Theme" > < item name = "android:windowTitleSize" >50dp</ item > < item name = "android:windowTitleBackgroundStyle" >@style/CustomWindowTitleBackground</ item > </ style > </ resources > |
在程序的android_manifest.xml中對應(yīng)activity中添加屬性android:theme = "@style/test" 就可以了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.guardian" android:versionCode = "1" android:versionName = "1.0" > < application android:icon = "@drawable/icon" android:label = "@string/app_name" > < activity android:name = ".Test" android:label = "@string/app_name" android:theme = "@style/test" //就在這里 > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > < uses-sdk android:minSdkVersion = "4" /> </ manifest > |
之后借助于設(shè)置自定義的標(biāo)題欄xml文件,就可以自定義標(biāo)題欄布局了