我同意不完全支持此操作,但这就是我所做的。您可以将自定义视图用于操作栏(它将显示在图标和操作项之间)。我正在使用自定义视图,并且已禁用本机标题。我所有的活动都继承自一个活动,该活动在onCreate中包含以下代码:
this.getActionBar().setDisplayShowCustomEnabled(true);this.getActionBar().setDisplayShowTitleEnabled(false);LayoutInflater inflator = LayoutInflater.from(this);View v = inflator.inflate(R.layout.titleview, null);//if you need to customize anything else about the text, do it here.//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title((TextView)v.findViewById(R.id.title)).setText(this.getTitle());//assign the view to the actionbarthis.getActionBar().setCustomView(v);
我的布局xml(上面的代码中为R.layout.titleview)如下所示:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" ><com.your.package.CustomTextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:textSize="20dp" android:maxLines="1" android:ellipsize="end" android:text="" /></RelativeLayout>



