2012-10-30 38 views
10

使用相對佈局時,ActionBar不顯示標題。 而當我不使用RelativeLayout時,這些項目沒有對齊到正確的位置。 請不要告訴我使用菜單項作爲解決方案。它必須是自定義的。帶有自定義RelativeLayout的ActionBarSherlock不顯示標題文本

這是我如何設置:

ActionBar bar = getSupportActionBar(); 
    bar.setDisplayOptions(
      ActionBar.DISPLAY_HOME_AS_UP | 
      ActionBar.DISPLAY_SHOW_CUSTOM | 
      ActionBar.DISPLAY_SHOW_HOME | 
      ActionBar.DISPLAY_SHOW_TITLE | 
      ActionBar.DISPLAY_USE_LOGO); 

    bar.setTitle(title); 
    bar.setCustomView(actionBar); 
    this.setTitle(title); 

這是當對齊是好的,但標題沒有顯示

<?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"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" > 

     <ImageButton 
      android:id="@+id/acion_add" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/menu_label_add" 
      style="?attr/actionButtonStyle" /> 

     <ImageButton 
      android:id="@+id/action_prev" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/menu_label_prev" 
      style="?attr/actionButtonStyle" /> 

     <ImageButton 
      android:id="@+id/action_next" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/menu_label_next" 
      style="?attr/actionButtonStyle" /> 

    </LinearLayout> 

</RelativeLayout> 

Screenshot

而且來這裏的佈局,其中的標題顯示,但項目對齊到左側

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

    <ImageButton 
     android:id="@+id/acion_add" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/menu_label_add" 
     style="?attr/actionButtonStyle" /> 

    <ImageButton 
     android:id="@+id/action_prev" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/menu_label_prev" 
     style="?attr/actionButtonStyle" /> 

    <ImageButton 
     android:id="@+id/action_next" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/menu_label_next" 
     style="?attr/actionButtonStyle" /> 

</LinearLayout> 

Screenshot

回答

12

我有同樣的問題,同時發現此鏈接:https://groups.google.com/forum/?fromgroups=#!topic/actionbarsherlock/He6gst4nvR0

這爲我工作:

佈局:

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

    ... 

</LinearLayout> 

代碼:

import com.actionbarsherlock.app.ActionBar.LayoutParams; 
... 
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL); 
getSupportActionBar().setCustomView(segmentView, lp); 
0

有一個解決方法是將TextView和titleTextStyle添加到佈局。 如果您有任何更好的答案,我期待着看到它。

我的解決方法:

<?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"> 

    <TextView 
     android:id="@+id/player_title" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_toLeftOf="@+id/buttons_holder" 
     android:ellipsize="end" 
     style="@style/TextAppearance.Sherlock.Widget.ActionBar.Title"/> 

    <LinearLayout 
     android:id="@+id/buttons_holder" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" > 

     <ImageButton 
      android:id="@+id/acion_add" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/menu_label_add" 
      style="?attr/actionButtonStyle" /> 

     <ImageButton 
      android:id="@+id/action_prev" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/menu_label_prev" 
      style="?attr/actionButtonStyle" /> 

     <ImageButton 
      android:id="@+id/action_next" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/menu_label_next" 
      style="?attr/actionButtonStyle" /> 

    </LinearLayout> 

</RelativeLayout> 
+0

我做了同樣的事情。不幸的是,這在RTL設備中無法正常工作。 – AsafK

相關問題