安卓:获取视图引用到菜单项视图、菜单项

2023-09-04 06:32:57 作者:一鹿都晗着暖阳

我打算使用快速行动UI模式在我的应用程序。 Android的快速操作的用户界面模式。快速操作窗口需要一个支点,以坚持。

I plan to use quick actions UI pattern in my application. Android Quick Actions UI Pattern . The quick action window needs a pivot view to stick to.

    quickAction.show(View pivotView);

我打算用快速行动的菜单项目,我可以访问被点击的项目。 但问题是,我需要从菜单项中引用视图,这样我可以把它传递给迅速采取行动。

I intend to use quick action for the menu Item, I can get access to the item that is clicked. But the problem is i need to reference a view from the menu item so that i can pass it to the quick action.

我怎样才能参考所选的菜单项的景色。

How can i get reference to a view in the menuItem that is selected.

推荐答案

您可以通过提供您的菜单项,在XML的actionViewClass性能达到这一点,那么你将能够获得枢轴视图u想。在code会是这样

You can achieve this by providing your menu item with an actionViewClass property in xml and then you will be able to get the pivot view u wanted. The code would be something like this

<item
    android:id="@+id/menu_find"
    android:showAsAction="ifRoom"
    android:actionViewClass="android.widget.ImageButton"
    />

在您OnCreateOptionsMenu做到这一点

In your OnCreateOptionsMenu do this

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu_search, menu);
    locButton = (ImageButton) menu.findItem(R.id.menu_find).getActionView();
    locButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            createPopup();
            mQuickAction.show(v);
        }
    });
    return true;
}