原裝的Android標(biāo)題欄配色比較單調(diào),就是黑色的一坨,現(xiàn)在假設(shè)你的軟件需要獨(dú)自添加標(biāo)題欄,這樣不僅美觀而且可以將進(jìn)度條等加進(jìn)去,如何實(shí)現(xiàn):
方法一、在你的那張Activity中onCreate方法中加上下面代碼:
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ā)生變化,那有沒有好的方法呢?答案是有的、
方法二:
因此先定義一個(gè)style,若修改背景請(qǐng)修改android:windowTitleBackgroundStyle
若修改標(biāo)題欄高度,請(qǐng)修改android:windowTitleSize
例子:
<?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中對(duì)應(yīng)activity中添加屬性android:theme = "@style/test" 就可以了
<?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)題欄布局了
新聞熱點(diǎn)
疑難解答
圖片精選