2012-10-30 25 views
10

Thanh tác vụ không hiển thị tiêu đề khi sử dụng bố cục tương đối. Và khi tôi không sử dụng RelativeLayout, các mục không được căn chỉnh sang phải. Vui lòng không cho tôi biết sử dụng các mục menu làm giải pháp. Nó phải là tùy chỉnh.ActionBarSherlock với RelativeLayout tùy chỉnh không hiển thị văn bản tiêu đề

Sau đây là cách tôi đặt nó:

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); 

Đây là khi các class là tốt, nhưng tiêu đề không được hiển thị

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

Và ở đây có bố trí nơi tiêu đề được hiển thị, nhưng các mục được căn chỉnh sang trái

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

Trả lời

12

tôi đã cùng một vấn đề và tìm thấy liên kết này: https://groups.google.com/forum/?fromgroups=#!topic/actionbarsherlock/He6gst4nvR0

này đã làm việc cho tôi:

bố trí:

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

mã:

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

Có một workaround bằng cách thêm TextView với titleTextStyle để bố trí. Nếu có ai trong số các bạn có câu trả lời hay hơn thì tôi rất mong được thấy nó.

workaround của tôi:

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

Tôi đã làm điều tương tự. Thật không may điều này không hoạt động đúng trong các thiết bị RTL. – AsafK

Các vấn đề liên quan