2012-09-21 59 views
1

我想創建ActionBar配合圖標,標籤和菜單按鈕在同一行,但通過下面的代碼,我得到2線爲上PRINTSCREEN:動作條與圖標,標籤和菜單按鈕在一行

的java:

viewPager = (ViewPager) findViewById(R.id.pager); 
bar = getSupportActionBar(); 
bar.setIcon(R.drawable.ic_map); 
bar.setDisplayShowHomeEnabled(false); 
bar.setDisplayShowTitleEnabled(false); 
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

TabsAdapter tabsAdapter = new TabsAdapter(this, viewPager); 
tabsAdapter.addTab(bar.newTab().setText("A"), ClientTemplates.class, null); 
tabsAdapter.addTab(bar.newTab().setText("B"), ClientWalletsCards.class, null); 
tabsAdapter.addTab(bar.newTab().setText("C"), ClientReports.class, null); 

XML:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

<android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:paddingRight="1px" 
     android:paddingLeft="1px"/> 

我必須在只有1行的代碼中更改什麼?

回答

2

據我所知,您對這個沒有控制權。 Android(或者在這種情況下,ActionBarSherlock)會將動作欄選項卡放在需要的位置,甚至可以根據需要將它們從選項卡轉換爲下拉列表。

如果您使用android:uiOptions="splitActionBarWhenNarrow",您可以將溢出菜單按鈕行放在屏幕的底部。

+2

應該指出的是,這種行爲(標籤出現在操作欄上方)是一個錯誤Android系統。爲了一致性,ActionBarSherlock反映了這個錯誤。 –

0

動作條CustomView是實現ü希望的效果,自行製作一些標籤,像這樣的一個好辦法:

<?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="match_parent" 
    android:layout_gravity="right"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <RelativeLayout 
      android:id="@+id/tabone" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="@dimen/actionbar_margin"> 
      <ImageView 
       android:contentDescription="@string/tabone_discription" 
       android:id="@+id/mainpage" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/account_dark" 
       android:layout_centerInParent="true" 
       /> 
     </RelativeLayout> 

     <ImageView 
      android:contentDescription="@string/divider_discription" 
      android:id="@+id/divider1" 
      android:background="@drawable/list_divider_holo_light" 
      android:layout_height="match_parent" 
      android:layout_marginTop="@dimen/divider_margin_top_bottom" 
      android:layout_marginBottom="@dimen/divider_margin_top_bottom" 
      android:layout_toRightOf="@id/tabone" 
      android:layout_marginLeft="@dimen/actionbar_margin" 
      android:layout_width="@dimen/divider_width"/> 

     <RelativeLayout 
      android:id="@+id/tabtwo" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_toRightOf="@id/divider1" 
      android:layout_marginLeft="@dimen/actionbar_margin"> 
      <ImageView 
       android:contentDescription="@string/tabtwo_discription" 
       android:id="@+id/atpage" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/at_dark" 
       android:layout_centerInParent="true" 
       /> 
     </RelativeLayout> 

     <ImageView 
      android:contentDescription="@string/divider_discription" 
      android:id="@+id/divider2" 
      android:background="@drawable/list_divider_holo_light" 
      android:layout_height="match_parent" 
      android:layout_marginTop="@dimen/divider_margin_top_bottom" 
      android:layout_marginBottom="@dimen/divider_margin_top_bottom" 
      android:layout_marginLeft="@dimen/actionbar_margin" 
      android:layout_toRightOf="@id/tabtwo" 
      android:layout_width="@dimen/divider_width"/> 

     <RelativeLayout 
      android:id="@+id/tabthree" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_toRightOf="@id/divider2" 
      android:layout_marginLeft="@dimen/actionbar_margin"> 
      <ImageView 
       android:contentDescription="@string/tabthree_discription" 
       android:id="@+id/replaypage" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/comment_dark" 
       android:layout_centerInParent="true" 
       /> 
     </RelativeLayout> 

     <View 
      android:layout_height="0dp" 
      android:layout_width="0dp" 
      android:layout_toRightOf="@id/tabthree" 
      android:layout_marginLeft="12dp"/> 

    </RelativeLayout> 

    <ImageView 
     android:contentDescription="@string/actionbar_line" 
     android:layout_height="3dp" 
     android:layout_width="0dp" 
     android:id="@+id/actionbarline" 
     android:background="@drawable/ab_solid_custom_blue_inverse_holo" 
     /> 

</RelativeLayout> 

,然後在您的活動的Java文件:

mActionBar.setDisplayShowCustomEnabled(true); 
mActionBar.setCustomView(R.layout.ab_customview); 

我沒有使用標籤,但結果看起來像我的行動條的同一行上有三個標籤:)