原裝的Android標題欄配色比較單調,就是黑色的一坨,現在假設你的軟件需要獨自添加標題欄,這樣不僅美觀而且可以將進度條等加進去,如何實現:
方法一、在你的那張Activity中onCreate方法中加上下面代碼:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); //軟件activity的布局 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); //titlebar為自己標題欄的布局
但是新的問題又來了,這樣是無法深層的定制標題欄的,比如原有的高度和背景都沒有發生變化,那有沒有好的方法呢?答案是有的、
方法二:
因此先定義一個style,若修改背景請修改android:windowTitleBackgroundStyle
若修改標題欄高度,請修改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中對應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>
之后借助于設置自定義的標題欄xml文件,就可以自定義標題欄布局了
新聞熱點
疑難解答
圖片精選