声明,使用居中标题的ActionBar会严重影响Android平台的UI的一致性,不建议使用,需要的时候假冒偶尔使用一下,本人不建议使用这种方式

Activity的OnCreate方法中

 

View actionbar_title = LayoutInflater.from(this).inflate(
                R.layout.actionbar_title, null);

        title = (TextView) actionbar_title.findViewById(R.id.title);

        button_back = (Button) actionbar_title.findViewById(R.id.button_back);
        button_right = (Button) actionbar_title.findViewById(R.id.button_right);
        params = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.MATCH_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        getSupportActionBar().setCustomView(actionbar_title, params);
        getSupportActionBar().setDisplayShowCustomEnabled(true);

R.layout.actionbar_title的具体代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/title_bg"
    android:gravity="center"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <Button
        android:id="@+id/button_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/back_selector"
        android:visibility="gone" />

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:singleLine="true"
        android:text="Loading"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#4e5762" />

    <Button
        android:id="@+id/button_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_marginTop="3dp"
        android:background="@drawable/demo_button_selector"
        android:gravity="center"
        android:visibility="gone" />

</LinearLayout>

最后的截图了

device-2012-11-15-141637