Android中自定义标题栏样式的两种方法
原装的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
例子:
<?xmlversion="1.0"encoding="utf-8"?> <resourcesxmlns:android="http://schemas.android.com/apk/res/android"> <stylename="CustomWindowTitleBackground"> <itemname="android:background">#565656</item> </style> <stylename="test"parent="android:Theme"> <itemname="android:windowTitleSize">50dp</item> <itemname="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> </style> </resources>
在程序的android_manifest.xml中对应activity中添加属性android:theme="@style/test"就可以了
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.guardian" android:versionCode="1" android:versionName="1.0"> <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"> <activityandroid:name=".Test" android:label="@string/app_name" android:theme="@style/test"//就在这里 > <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> <uses-sdkandroid:minSdkVersion="4"/> </manifest>
之后借助于设置自定义的标题栏xml文件,就可以自定义标题栏布局了