1、建立title所需的布局文件:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal"
>
<Button android:text=" 返回 "
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_middle_normal"
android:layout_marginLeft="20dp"
/>
<TextView android:text=" 欢迎使用 "
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:typeface="monospace"
android:layout_marginLeft="15dp"
/>
<Button android:text=" 下一步 "
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_middle_normal"
android:textSize="15dp"
android:typeface="monospace"
android:layout_marginLeft="15dp"
/>
</LinearLayout>
2、在java代码部分进行设置
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 注意顺序
setContentView(R.layout.declaration); // 注意顺序
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, // 注意顺序
R.layout.title);
}
到此步骤可以修改标题栏,但是不能设置标题栏的大小和背景。还需要处理
3、编写style.xml
<?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>
4在清单文件中配置
<activity android:name=".DeclarationActivity"
android:label="@string/app_name"
android:theme="@style/test"
>
这样可以完成配置了。如果想完成更复杂的标题栏,那么需要通过java代码进行自定义。
载自:http://hi.baidu.com/wudaovip/blog/item/da4b8b1bd4ea6d0b8718bf4c.html