效果如图:
先在strings.xml 中定义主题
<style name="customTitlebg" >
<item name="android:background">@drawable/title_bg</item>
</style>
<style name="titlebar" parent="android:Theme">
<item name="android:windowTitleSize">40dp</item>
<item name="android:windowTitleBackgroundStyle">@style/customTitlebg</item>
</style>
在AndroidManifest.xml,application标签中改为使用我们自定义的主题
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/titlebar" >
<activity
android:name=".MainActivity"
...
下面是自定义标题栏的实现 title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:src="@drawable/title_home_normal" />
<TextView android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="自定义标题栏" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/title_new_normal" />
</RelativeLayout>
最后修改Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
setContentView(R.layout.tabhost);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);//自定义布局赋值
}