如何将ActionBar项目放置在主ActionBar和底部栏中

20

在此输入图片描述

谷歌+应用程序的布局中,在主操作栏和底部都有动作栏项目。目前,我正在使用android:uiOptions="splitActionBarWhenNarrow" 将项目放置在底部栏中。如何将项目放置在顶部和底部两个位置?


是的,如何在顶部显示溢出图标? - samirprogrammer
5个回答

26

希望我的回答对你还不算太晚。

  1. 使用android:uiOptions="splitActionBarWhenNarrow"(这会将一些东西添加到底部栏)。
  2. 创建像下面代码这样的新布局(此布局将处理顶部栏中的所有项目)。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right" >
    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
    <ImageButton
        android:id="@+id/action_starred"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/actionButtonStyle"
        android:src="@android:drawable/ic_menu_compass" 
        android:onClick="FakeMenu"/>
    </LinearLayout>
    
  3. 将此粘贴到您的活动中

  4. ActionBar actionBar = getActionBar();               
    actionBar.setCustomView(R.layout.actionbar_top); //load your layout
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_CUSTOM); //show it 
    
  5. 就这些 :)


你帮了我很多,谢谢!对于使用ActionBarSherlock库的人有个小提示 - 你可以通过getSherlock().getActionBar()获取你的Action Bar对象。 - lopek

11

我认为使用标准的ActionBar是不可能实现的。当启用分割ActionBar时,所有操作都会出现在窄屏幕下方。

Google+应用程序“创建帖子”活动中的底部栏似乎是自定义实现的。请注意,长按以显示操作标签的功能不起作用,并且即使切换到横向方向,底部栏仍然存在。位置项是一个切换开关,这也不是标准ActionBar的行为。


2
我遇到了同样的问题,但是我发现没有什么方法真正有效,因为我需要处理操作栏的狭窄,在大屏幕设备上,按钮会放置在顶部而不是底部。所以,最终我通过在主布局中放置一个“页脚”来解决这个问题:

Bottom ActionBar Example

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="false"
        android:layout_above="@+id/footer">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:id="@+id/container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/body_padding_large"
            android:paddingRight="@dimen/body_padding_large"
            android:paddingTop="@dimen/body_padding_medium"
            android:paddingBottom="@dimen/body_padding_medium"
            android:gravity="top">

        </LinearLayout>

    </ScrollView>
    <LinearLayout android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        style="@android:style/ButtonBar">
        <Button android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 1" />

        <Button android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 2" />
    </LinearLayout>
</RelativeLayout>

0

当您拥有的项目超过顶部栏可以显示的项目时,底部栏将被填充。

与其显示“溢出”三个点,项目将放置在底部栏上。请记得使用:

android:showAsAction="ifRoom|withText"

在XML中设置您的菜单项。

了解更多信息: http://developer.android.com/guide/topics/ui/actionbar.html#SplitBar


那似乎不对。在Google+的例子中,他们也在顶部放置了溢出图标。 - PolandSpring
1
@kkshin 这是一个自定义视图,而不是默认的分割操作栏。 - doorstuck

0
<?xml version="1.0" encoding="utf-8"?>

< LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right" >

<Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"/>

<ImageButton
    android:id="@+id/action_starred"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/actionButtonStyle"
    android:src="@android:drawable/ic_menu_compass" 
    android:onClick="FakeMenu"/>

</LinearLayout>









//Then put these lines of code in your java class or activity

    ActionBar actionBar = getActionBar();               
    actionBar.setCustomView(R.layout.actionbar); //load your layout
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_USTOM); //show it 

// 希望这有所帮助


在这种情况下,如果您有权利,可以为该答案投票(如果不需要评论)。但是,如果您找到了解决问题的另一种方法,请随时分享。 - TocToc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接