2014-02-14 29 views
9

我嘗試了我自己的自定義操作欄,以獲取以中間爲中心的操作欄標題。有效。但是,在操作欄中添加了選項菜單後,標題再次向左移動。我應該怎麼做才能讓標題集中在操作欄的中間位置? 我的代碼是:如何讓android動作條標題中心對齊?

ActionBar actionBar = getActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(false); 
    actionBar.setDisplayShowCustomEnabled(true); 
    actionBar.setDisplayShowTitleEnabled(false);  
    actionBar.setDisplayUseLogoEnabled(false); 
    actionBar.setDisplayShowHomeEnabled(false); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    View cView = getLayoutInflater().inflate(R.layout.custom_actionbar, null); 
    actionBar.setCustomView(cView); 

和我的佈局是

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:background="@android:color/holo_blue_light" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/apptitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:layout_gravity="center_horizontal|center_vertical" 
    android:gravity="center_horizontal|center_vertical" 
    android:text="@string/app_name" /> 

+0

你可以發佈你的代碼,你試圖集中標題? –

+0

http://stackoverflow.com/a/23841756/896322這裏也許你可以找到解決方案 –

回答

15

我相信,您使用下面的設置你的自定義主題。

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.abs_layout); 

如果不是,請添加第一行。並且您的自定義佈局中的文本居中。 (Gravity = Center)

我還發現了類似的鏈接herehere。希望這可以幫助。

試試這個。

我能夠完成我想要的而不是顯示活動的標題 ,而是按照您的建議使用customView。我錯過了一段時間的關鍵 是您必須使用setCustomView(View, ActionVar.LayoutParams)方法以獲得定製視圖 居中,只需在自定義視圖 佈局文件中設置layout_gravity =「center」即可不行。下面是說明如何我 得到它的工作很短的代碼片段:

  TextView customView = (TextView) 
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered, 
null); 
      ActionBar.LayoutParams params = new 
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, 
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER); 

      customView.setText("Some centered text"); 
      getSupportActionBar().setCustomView(customView, params); 

此方法爲我工作至少3.2,2.3和2.2的設備。

來源https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ

+0

我試過了。但它不工作。 – Anu

+0

您是否也檢查鏈接中的解決方案?並且,在你的RelativeLayout中嘗試設置android:layout_width =「match_parent」。 –

+0

這可以工作,但會刪除導航菜單的按鈕。在Android的ActionBar血腥的惡臭,所以我一直懷疑,我最終不得不推出自己的... –

2

首先,創建一個.xml文件是這樣的:(action_bar.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

<TextView 
    android:id="@+id/actionbar_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:maxLines="1" 
    android:clickable="false" 
    android:focusable="false" 
    android:longClickable="false" 
    android:textStyle="bold" 
    android:textSize="18sp" 
    android:textColor="#000000" /> 

</LinearLayout> 

,然後在你的活動:

View view = getLayoutInflater().inflate(R.layout.action_bar, null); 
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
      ActionBar.LayoutParams.WRAP_CONTENT, 
      ActionBar.LayoutParams.MATCH_PARENT, 
      Gravity.CENTER); 

    TextView Title = (TextView) view.findViewById(R.id.actionbar_title); 
    Title.setText("Your Title"); 

getSupportActionBar().setCustomView(view,params);  
getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title 
getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title 

我試着很多解決方案,只有這一個工程。希望它可以幫助你。