2013-05-13 21 views
4

我想將標題居中在動作欄中,同時在其旁邊有動作圖標(並忽略它們以用於重力設置)。以按鈕爲中心的動作欄標題

我想有這樣的: enter image description here

相反,我有這樣的: enter image description here

這裏是我的代碼:

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可供選擇的按鈕(現在它也可以調整居中)。

任何幫助,將不勝感激。

我不想實現我自己的操作欄項目/按鈕邏輯。

回答

3

您可以在TextView中試試這個:

<TextView 
    android:id="@+id/actionbar_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:maxHeight="1dp" 
    android:maxLines="1" 
    android:paddingLeft="48dp" 
    android:paddingRight="48dp" 
    android:text="Bacon ipsum" 
    android:textColor="#777777" 
    android:textSize="20sp" /> 
+1

不幸的是,我不能只設置填充並將其留在那裏,我必須繼續打開和關閉,具體取決於是否在其右側有一個操作項目。哦,這比我自己實現按鈕要少得多,所以我認爲這將是答案。謝謝。 – Tamas 2013-05-14 06:22:33

0

您是否試圖將此代碼實現爲文本?

android:layout_centerHorizontal="true" 
+0

的問題是,該行動項目佔用空間旁邊的自定義視圖,這樣即使我在佈局居中,我仍然有與玩填充,就像user2203377建議的一樣。 – Tamas 2013-05-14 06:41:39

2
  1. 從你的RelativeLayout移除啓動圖標。
  2. 更改的RelativeLayout的寬度wrap_content
  3. 卸下:

    actionBar.setDisplayShowCustomEnabled(真); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowTitleEnabled(false);

  4. 地址:

    ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
    
    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER); 
    
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM) 
    actionBar.setCustomView(yourInflatedCustomView, lp); 
    
+0

這應該是被接受的答案。塔馬斯,你應該給它一個機會:)只是缺少一行:ActionBar#setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)。 – 2015-05-19 14:08:29

+0

它也適用於我。不信...我爲此付出了代價。乾杯 – blub 2016-06-15 17:46:10