效果图如下所示:
实现方法步骤如下:
1、在layout下创建一个title.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我的自定义标题栏" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:layout_centerInParent="true"
android:layout_alignParentRight="true"
android:src="@drawable/next" />
</RelativeLayout>
注意:
a)要使用RelativeLayout的布局
b)右边按钮的属性需要指定layout_centerInParent="true"
2、在MainActivity.java中的onCreate中增加下面的代码:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebtn);
注意: 以上三行顺序不能颠倒!