编程显示动作条上溢 [英] Programmatically display actionbar overflow

查看:172
本文介绍了编程显示动作条上溢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个菜单的动作条。当您单击按钮,菜单显示出来,一切都很好与世界同步。

I have an actionbar with a menu. When you click the button, the menu shows up and all is well with the world.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/reload" 
        android:title="@string/reload" 
        android:alphabeticShortcut="r" 
        android:numericShortcut="1" 
        android:titleCondensed="Rload"></item>

    <group android:id="@+id/adv" android:menuCategory="secondary">
        ...options
    </group>
</menu>

不过,我刚告知,我们有一个客户,其设备具有某种被显示在当前,使他们不能访问菜单等菜单栏。所以我增加了一个按钮上的WebView与javascriptinterface告诉我的活动以编程方式显示菜单。我能到那里就好了,但我不能为我的生活弄清楚如何显示菜单,如果菜单按钮为pressed。

However, I just got told that we have a client whose device has some sort of other menu bar that is displayed over the current one so they can't access the menu. So I added a button on the webview with a javascriptinterface to tell my Activity to display the menu programmatically. I can get there just fine, but I can't for the life of me figure out how to display the menu as if the menu button was pressed.

我试图做这样的事情:

private void showMenu() {
    try{
        actionbar.show();
        MenuItem menu = (MenuItem) findViewById(R.id.adv);
        menu.expandActionView();
    }
    catch(Exception e){
        Log.e(LOG_CAT,"Unable to inflate menu");
    }
}

但我得到一个空指针异常

but I'm getting a null pointer exception

java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.MenuItem.expandActionView()' on a null object reference

这似乎是我想要做的应该是很轻松的,但它可能是我只是在做一些愚蠢的,而不是。

It seems like what I'm trying to do should be trivially easy, but it may be that I'm just doing something boneheaded instead.

修改我真的不希望只是显示R.id.adv,我想显示整个菜单,这样也许应该是 findViewById(R.menu .menu_main)来代替,其中 menu_main 是上面的XML中定义的文件的名称。

EDIT I don't actually want to just show R.id.adv, I would like to show the whole menu so that should probably be findViewById(R.menu.menu_main) instead, where menu_main is the name of the file that the above xml is defined in.

推荐答案

findViewById 看在活动中,没有菜单的视图层次。你需要得到你需要正确后,创建了菜单中的项目:

findViewById looks in the view hierarchy of the activity, not the menu. You need to get the items you need right after your menu is created:

编辑:

没想到这是可能的,但它是。随着这个答案这里是你如何能做到这一点:

Didn't think it's possible but it is. With the help of this answer here's how you can do it:

View mOverflow;

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

    final View decor = getWindow().getDecorView();
    decor.post(new Runnable() {
        @Override
        public void run() {
            ArrayList<View> results = new ArrayList<>();
            decor.findViewsWithText(results, "OVERFLOW",
                    View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);

            if (results.size() == 1) {
                mOverflow = results.get(0);
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

private void showMenu() {
    actionbar.show();
    mOverflow.performClick();
}

而在你的 styles.xml

<style name="AppTheme" parent="android:Theme.Holo">
    <item name="android:actionOverflowButtonStyle">@style/Widget.ActionButton.Overflow</item>
</style>

<style name="Widget.ActionButton.Overflow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
    <item name="android:contentDescription">OVERFLOW</item>
</style>

findViewsWithText 要求API 14+,但是答案我联系,具有更低的API的解决方案。

findViewsWithText requires API 14+, however the answer I linked, has a solution for lower APIs.

这篇关于编程显示动作条上溢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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