简体   繁体   English

以编程方式显示操作栏溢出

[英]Programmatically display actionbar overflow

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>

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. 因此,我在Web视图上添加了带有javascript界面​​的按钮,以告诉我的Activity以编程方式显示菜单。 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. 我可以到达那里,但是我一生都无法弄清楚如何显示菜单,就像按下菜单按钮一样。

I tried doing something like this: 我试图做这样的事情:

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. 似乎我想做的事很容易,但是可能我只是在做一些笨拙的事情。

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. 编辑我实际上并不想只显示R.id.adv,我想显示整个菜单,因此应该改为findViewById(R.menu.menu_main) ,其中menu_main是上述文件的名称定义为xml。

findViewById looks in the view hierarchy of the activity, not the menu. findViewById查找活动的视图层次结构,而不是菜单。 You need to get the items you need right after your menu is created: 创建菜单后,您需要立即获取所需的项目:

Edit: 编辑:

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();
}

And in your styles.xml : 并在您的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 requires API 14+, however the answer I linked, has a solution for lower APIs. findViewsWithText需要API 14+,但是我链接的答案为较低的API提供了解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM