设置菜单项目为中心,全宽与ActionBarSherlock [英] Set menu items centered and full-width with ActionBarSherlock

查看:165
本文介绍了设置菜单项目为中心,全宽与ActionBarSherlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示两个项目的操作杆,取消和保存,但我想这些奠定了跨越操作栏的整个长度,像这样:

I'm attempting to display an action bar with two items, Cancel and Save, but I want these laid out to span the entire length of the action bar, like so:

______________________________
|____Cancel____|____Save ____|
|                            |
|                            |
|                            |
|                            |

所以基本上我需要一种方法来显示只有两个行动项目,居中,并跨越整个操作栏。

So basically I need a way to display only two action items, centered, and spanning the entire action bar.

什么是实现这一目标的最简单的方法?我正在考虑使用customlayout,但我还是新的使用和其他动作菜单,我不知道我怎么会能够做到这一点,有这些还是菜单操作项目。

What's the easiest way to accomplish this? I'm considering using a customlayout, but as I'm still new to using actionbars and menus, I'm not sure how I would be able to do this and have these still be menu action items.

是否有某种造型的解决方案?什么是实现这一目标的最佳和最无痛的方式?

Is there some kind of styling solution? What's the best and most painless way to accomplish this?

修改

由于这是我第一次去的动作条和自定义布局,我没有感受什么工作,什么是这根本行不通绝望愚蠢的做法。例如,自定义布局设置这样的:

As this is my first go with Action Bars and custom layouts, I've got no feel for what will work and what is a hopelessly idiotic approach that simply won't work. For example, setting a custom layout to this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_gravity="center_horizontal">

    <item android:id="@+id/menu_item_cancel"
            android:showAsAction="always"
            android:title="Cancel"/>

    <item android:id="@+id/menu_item_save"
            android:showAsAction="always"
            android:title="Save" />
</menu>

将导致通胀例外,将无法正常工作,虽然其他人有更多的经验将rightaway知道这可不行,我留下笨拙燃烧左右的时间,直到我能感觉到我的出路这个还是有人点我正确的方向。

causes an inflation exception and won't work, and though others with more experience will rightaway know this can't work, I'm left bungling about burning time until I can feel my way out of this or someone points me in the right direction.

因此​​,对于那些问我什么,我已经试过,我会继续发帖,但预计大致相同的。

So for those asking me what I've tried, I'll keep posting, but expect much of the same.

第二次试

获取一个ClassNotFoundException:找不到类android.view.menu的路径

Getting a ClassNotFoundException: Didn't find class "android.view.menu" on path

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal">

    <menu android:layout_gravity="center_horizontal">

        <item android:id="@+id/menu_item_cancel"
                android:showAsAction="always"
                android:title="Cancel"/>

        <item android:id="@+id/menu_item_save"
                android:showAsAction="always"
                android:title="Save" />
    </menu>

</LinearLayout>

第三试

这几乎工作,但我只看到各个按钮的上半部分。下半部分被切断,即使操作栏似乎有足够的空间,它本身正确地画出。

This almost works, except I only see the top half of each button. The lower half is cut off, even though the action bar seems to have enough room for it to draw itself properly.

我也失去了行动项目的造型,因为这些都是正常的按钮,以及分频器。我宁愿发现,使用本机的行动项目,因为它会更容易随着动作条的整个应用程序其余样式的解决方案。

I also lose the action item styling since these are normal buttons, as well as the divider. I'd much rather find a solution that uses the native action items, as it will make it easier to style along with the rest of the action bars throughout the app.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal">

                <Button
                    android:id="@+id/cancel_btn"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="Cancel" />

                <Button
                    android:id="@+id/save_btn"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="Save" />
</LinearLayout>

第四次尝试

这一个是pretty接近。我正想通过ActionBarSherlock示范项目,并注意到用来分割菜单栏时,布局趋于中心本身。所以,我恢复了我一直在使用原来的菜单,并添加showAsAction ifRoom值。

This one is pretty close. I was going through the ActionBarSherlock demo project, and noticed when the split menu bar is used, the layout tends to center itself. So I reverted to the original menu I've been using, and added the showAsAction ifRoom value.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/menu_item_cancel"
            android:showAsAction="ifRoom"
            android:title="Cancel"/>

    <item android:id="@+id/menu_item_save"
            android:showAsAction="ifRoom"
            android:title="Save"/>
</menu>

我还添加了这我在清单活动:

I also added this to my activity in the manifest:

android:uiOptions="splitActionBarWhenNarrow"

这给我确切的造型我想...但它在拆分操作栏。我想在这上面,而不是在底部。与Android 2.2工程模拟器中运行这个,但似乎居中两个选项的比例文本的长度,所以取消面积比保存面积大。

This gives me the exact styling I want...but it's in the split action bar. I want this at the top, not at the bottom. Running this on the emulator with Android 2.2 works, but seems to center the two options proportional to the length of the text, so the area for Cancel is larger than the area for Save.

如果有人知道一种方式来获得这个确切的造型,但在顶部操作栏中,这将解决一切。

If someone knows a way to get this exact styling but in the top action bar, that would fix everything.

推荐答案

似乎没有成为一个方式与本地操作的直板造型或选项来做到这一点。不得不使用自定义视图,类似我第三次尝试,与修订的造型和背景来模拟操作栏。

Doesn't seem to be a way to do this with native action bar styling or options. Had to use a custom view, similar to my third try, with revised styling and backgrounds to mimic the action bar.

这篇关于设置菜单项目为中心,全宽与ActionBarSherlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆