我想將標題居中在動作欄中,同時在其旁邊有動作圖標(並忽略它們以用於重力設置)。以按鈕爲中心的動作欄標題
我想有這樣的:
相反,我有這樣的:
這裏是我的代碼:
ActionBar actionBar = getSupportActionBar();
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top_bar));
actionBar.setCustomView(LayoutInflater.from(this).inflate(R.layout.actionbar_layout, null), params);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
我的佈局:
<?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:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="8dp"
android:src="@drawable/launcher_icon" />
<TextView
android:id="@+id/actionbar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxHeight="1dp"
android:maxLines="1"
android:paddingLeft="48dp"
android:paddingRight="48dp"
android:text="Bacon ipsum"
android:textColor="#777777"
android:textSize="20sp" />
</RelativeLayout>
所以我的問題是,將標題居中只能工作,如果我在它旁邊有0個操作欄項目,只要我添加一個項目,中心就會被搞砸。 這也很棒,如果我可以使用up up可供選擇的按鈕(現在它也可以調整居中)。
任何幫助,將不勝感激。
我不想實現我自己的操作欄項目/按鈕邏輯。
不幸的是,我不能只設置填充並將其留在那裏,我必須繼續打開和關閉,具體取決於是否在其右側有一個操作項目。哦,這比我自己實現按鈕要少得多,所以我認爲這將是答案。謝謝。 – Tamas 2013-05-14 06:22:33