如何获取菜单项的动作视图?

时间:2015-10-23 11:53:10

标签: android android-actionbar menuitem

这是我的代码

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>

我想查看&#34; menu_overflow&#34; 我该怎么做?

我试过以下方式:

Activity.java 代码

public boolean onCreateOptionsMenu(菜单菜单){

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

但是返回视图是 null

请帮助... thanx

4 个答案:

答案 0 :(得分:11)

你是否在onCreateOptionsMenu中夸大了菜单?看来,当执行onCreateOptionsMenu时,xml通胀会被添加到事件队列中,而不是立即膨胀。这是执行findViewById()时导致空指针的原因。尝试将视图查找放在Handler中。 Handler中的代码被添加到消息队列中,因此将在菜单膨胀后执行。这将解决您的空指针。它看起来像这样:

@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;
}

答案 1 :(得分:3)

您可以通过覆盖MenuItem方法获取对onPrepareOptionsMenu(Menu)的引用:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem actionViewItem = menu.findItem(R.id.your_menu_item);
    return super.onPrepareOptionsMenu(menu);
}

如果您想要引用ActionView,可以使用MenuItemCompat.getActionView(MenuItem)方法执行此操作:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem actionViewItem = menu.findItem(R.id.your_menu_item);
    View v = MenuItemCompat.getActionView(actionViewItem);
    return super.onPrepareOptionsMenu(menu);
}

答案 2 :(得分:0)

你试过这个吗?

@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);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        switch (item.getItemId()){
            case R.id.action_shopping_cart:
                Toast.makeText(this, "action_shopping_cart", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_notifications:
                Toast.makeText(this, "action_notifications", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_overflow:
                Toast.makeText(this, "menu_overflow", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_my_account:
                Toast.makeText(this, "action_my_account", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_current_orders:
                Toast.makeText(this, "action_current_orders", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_wish_list:
                Toast.makeText(this, "action_wish_list", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_contact_us:
                Toast.makeText(this, "action_contact_us", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_logout:
                Toast.makeText(this, "action_logout", Toast.LENGTH_SHORT).show();
                break;



        }



        return super.onOptionsItemSelected(item);
    }

注意xml的名称(即home.xml)

答案 3 :(得分:0)

尝试:

<item
    android:id="@+id/menu_overflow"
    android:orderInCategory="100"
    android:icon="@drawable/ic_action_overflow"
    android:title="OverFlow"
    app:showAsAction="always"
    app:actionViewClass="android.widget.ImageButton">

请注意以下几行:

    app:showAsAction="always"
    app:actionViewClass="android.widget.ImageButton"