【问题标题】:Actionbar with Icon, Tabs and MenuButton in one line一行中带有图标、选项卡和菜单按钮的操作栏
【发布时间】:2012-09-21 16:18:44
【问题描述】:

我想在一行中创建带有图标、选项卡和菜单按钮的ActionBar,但是通过使用下一个代码,我得到了两行,就像在 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 行代码,我必须对代码进行哪些更改?

【问题讨论】:

    标签: android actionbarsherlock


    【解决方案1】:

    AFAIK,你无法控制这个。 Android(或者,在这种情况下,ActionBarSherlock)会将操作栏选项卡放置在它想要的位置,甚至在需要时将它们从选项卡转换为下拉列表。

    如果您使用android:uiOptions="splitActionBarWhenNarrow",则可以将溢出菜单按钮线放在屏幕底部。

    【讨论】:

    • 应该注意这种行为(标签出现在操作栏上方)是Android中的一个错误。为了保持一致性,ActionBarSherlock 反映了这个错误。
    【解决方案2】:

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

    我没有使用标签,但结果看起来我的操作栏的同一行上有三个标签:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多