5

Tôi đang phát triển một ứng dụng Android, sử dụng thư viện ActionBarSherlock. Trong một hoạt động, tôi sử dụng điều hướng theo thẻ kết hợp với ActionBar bị thu gọn (các mục tác vụ ở dưới cùng).Buộc thu hẹp các tab trong ActionBar

Trong ảnh này, bạn có thể thấy Activity trong trạng thái hiện tại hiện tại: Các tab này đang được đẩy ở hàng thứ hai.

Current ActionBar


Trong bức ảnh sau đây bạn sẽ nhìn thấy Activity cách tôi muốn nó được: Các tab nên ở hàng đầu, chứ không phải trong một hàng thứ hai. Tôi đã đọc tài liệu ActionBarActionBarSherlock, nhưng không tìm thấy cách nào để buộc hành vi này.

The wished ActionBar layout

Đây là mã hiện tại, được sử dụng để tạo ra các ActionBar.

public class AdminActivity extends SherlockFragmentActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_admin); 

    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(true); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    ActionBar.Tab itemsTab = actionBar.newTab().setText(R.string.label_tab_items); 
    ActionBar.Tab usersTab = actionBar.newTab().setText(R.string.label_tab_users); 

    actionBar.addTab(itemsTab); 
    actionBar.addTab(usersTab); 
} 

Bất kỳ ý tưởng nào?

+1

Tiêu đề của bạn nói "ngăn chặn", nhưng câu hỏi của bạn cho thấy rằng, trên thực tế, bạn muốn 'Tabs' sụp đổ vào' ActionBar'. – adneal

+0

Tôi đã sửa nó, cảm ơn! – damaxxed

Trả lời

5

Có một sự phản ánh 'hack' để làm điều này. Tôi không nhận được tín dụng cho giải pháp mà tôi đã tìm thấy trong câu hỏi StackOverflow này replicate ActionBar Tab(s) with custom view.

//pre-ICS 
if (actionBarSherlock instanceof ActionBarImpl) { 
    enableEmbeddedTabs(actionBarSherlock); 

//ICS and forward 
} else if (actionBarSherlock instanceof ActionBarWrapper) { 
    try { 
     Field actionBarField = actionBarSherlock.getClass().getDeclaredField("mActionBar"); 
     actionBarField.setAccessible(true); 
     enableEmbeddedTabs(actionBarField.get(actionBarSherlock)); 
    } catch (Exception e) { 
     Log.e(TAG, "Error enabling embedded tabs", e); 
    } 
} 

//helper method 
private void enableEmbeddedTabs(Object actionBar) { 
    try { 
     Method setHasEmbeddedTabsMethod = actionBar.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class); 
     setHasEmbeddedTabsMethod.setAccessible(true); 
     setHasEmbeddedTabsMethod.invoke(actionBar, true); 
    } catch (Exception e) { 
     Log.e(TAG, "Error marking actionbar embedded", e); 
    } 
} 

Xem thêm bài viết trên blog này: http://sparetimedev.blogspot.co.uk/2012/11/forcing-embedded-tabs-in-actionbar.html

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