如何获取菜单项的动作视图? [英] How To Get Action View Of Menu Item?

查看:70
本文介绍了如何获取菜单项的动作视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

home.xml

<item
    android:id="@+id/action_shopping_cart"
    android:actionLayout="@layout/action_bar_cart_button"
    android:showAsAction="ifRoom"
    android:title="Shopping Cart" />
<item
    android:id="@+id/action_notifications"
    android:actionLayout="@layout/action_bar_notifications_button"
    android:showAsAction="ifRoom"
    android:title="Notifications" />
<item
    android:id="@+id/menu_overflow"
    android:orderInCategory="100"
    android:icon="@drawable/ic_action_overflow"
    android:showAsAction="always"
    android:title="OverFlow">

    <menu>
        <item
            android:id="@+id/action_my_account"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="My Account" />
        <item
            android:id="@+id/action_current_orders"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="My Orders" />
        <item
            android:id="@+id/action_wish_list"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="My Wishlist" />
        <item
            android:id="@+id/action_contact_us"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="Contact Us" />
        <item
            android:id="@+id/action_logout"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="Logout" />
    </menu>
</item>

我想要视图菜单溢出" ,该如何获取?

I want the View "menu_overflow" how can I get that?

我尝试了以下方式:

Activity.java 代码

public boolean onCreateOptionsMenu(菜单菜单){

public boolean onCreateOptionsMenu(Menu menu) {

      final MenuItem item = menu.findItem(R.id.menu_overflow);
      View view = (View) findViewById(R.id.menu_overflow);
      new MaterialShowcaseView.Builder(this)
                        .setTarget(mOverFlowIcon)
                        .setRadius(10)
                        .setMaskColour(Color.argb(150, 0, 0, 0))
                        .setContentText("Find Your Wishlist Here") // optional but starting animations immediately in onCreate can make them choppy
                        .setDismissOnTouch(true)
                        .show();
    return super.onCreateOptionsMenu(menu);
}

但是返回视图为.

请帮助...谢谢

推荐答案

您是否在onCreateOptionsMenu中扩展菜单?似乎在执行onCreateOptionsMenu时,会将xml膨胀添加到事件队列中,而不是立即膨胀.这是在执行findViewById()时导致空指针的原因.尝试将视图查找放入Handler中.处理程序中的代码将添加到消息队列中,因此将在菜单膨胀后执行.这将解决您的空指针.看起来像这样:

Are you inflating the menu in onCreateOptionsMenu? It appears that when onCreateOptionsMenu is executed, the xml inflation is added to the event queue, rather than inflating immediately. This is what causes your null pointer when you execute findViewById(). Try putting your view lookup inside a Handler. The code within the Handler is added to the message queue and will therefore be executed after the menu is inflated. This will resolve your null pointer. It will look something like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            View view = (View) findViewById(R.id.menu_overflow);
            new MaterialShowcaseView.Builder(this)
                    .setTarget(view)
                    .setRadius(10)
                    .setMaskColour(Color.argb(150, 0, 0, 0))
                    .setContentText("Find Your Wishlist Here") // optional but starting animations immediately in onCreate can make them choppy
                    .setDismissOnTouch(true)
                    .show();
        }
    });
    return true;
}

这篇关于如何获取菜单项的动作视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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