我计划在我的应用程序中使用快速动作UI模式。Android Quick Actions UI Pattern。 快速操作窗口需要一个基准视图以保持粘附。
quickAction.show(View pivotView);
我打算在菜单项中使用快速操作,我可以访问被点击的项目。
但问题是我需要从菜单项引用一个视图,以便将其传递给快速操作。如何获取所选menuItem中视图的引用?
我计划在我的应用程序中使用快速动作UI模式。Android Quick Actions UI Pattern。 快速操作窗口需要一个基准视图以保持粘附。
quickAction.show(View pivotView);
我打算在菜单项中使用快速操作,我可以访问被点击的项目。
但问题是我需要从菜单项引用一个视图,以便将其传递给快速操作。<item
android:id="@+id/menu_find"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.ImageButton"
/>
在你的 OnCreateOptionsMenu 中这样做
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;
}
这是一个老问题,但我最近遇到了 actionViewClass
属性的一些问题。对于以后也会遇到此类问题的人...
在 onOptionsItemSelected
中调用 findViewById(R.id.mnu_item)
会返回一个 View
锚点。
MenuItems
上的 QuickActions
不是很好的设计,但是我发现它们是实现带有自定义背景的子菜单的最简单方法。
findViewById()
方法调用必须在运行时执行。 - loadedion@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.section, menu);
new Handler().post(new Runnable() {
@Override
public void run() {
final View menuItemView = findViewById(R.id.action_preview);
// SOME OF YOUR TASK AFTER GETTING VIEW REFERENCE
}
});
return true;
}
对于任何想要查找菜单视图项的人(比如我),以下是一些更新。
如果您可以访问并使用AppCompat的工具栏,则有一种方法。这不是最有效的方法,但这是我发现的最简单的访问菜单项视图的方法。
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
// Find Menu
for (int toolbarChildIndex = 0; toolbarChildIndex < toolbar.getChildCount(); toolbarChildIndex++) {
View view = toolbar.getChildAt(toolbarChildIndex);
// Found Menu
if (view instanceof ActionMenuView) {
ActionMenuView menuView = (ActionMenuView) view;
// All menu items
for (int menuChildIndex = 0; menuChildIndex < menuView.getChildCount(); menuChildIndex++) {
ActionMenuItemView itemView = (ActionMenuItemView) menuView.getChildAt(menuChildIndex);
// Do something to itemView...
}
}
}
}
通用代码,也适用于Android 10
/**
* pass toolbar and menu item id, i.e. R.id.menu_refresh
*/
@Nullable
@Throws(
IllegalAccessException::class,
NoSuchFieldException::class
)
fun getMenuItemView(toolbar: Toolbar?, @IdRes menuItemId: Int): View? {
val mMenuView: Field = Toolbar::class.java.getDeclaredField("mMenuView")
mMenuView.setAccessible(true)
val menuView: Any? = mMenuView.get(toolbar)
(menuView as ViewGroup).children.forEach {
if(it.id == menuItemId) {
return it
}
}
return null
}
Kotlin!!
override fun onCreateOptionsMenu(Menu menu): Boolean {
/*Adding menu items to action bar*/
menuInflater.inflate(R.menu.main, menu)
/*Getting menu item*/
val locButton: MenuItem = menu.findItem(R.id.menu_find)
/*Creating click listener*/
locButton.setOnMenuItemClickListener{
/*TODO: Handle it*/
true
}
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// the id is of type int
int someId = item.getItemId();
// can use an if() or switch() statement to check if id is selected
//a Toast message can be used to show item is selected
}