使用操作栏主页按钮为PopupMenu的锚视图 [英] Using Action Bar Home Button as an anchor view for PopupMenu

查看:217
本文介绍了使用操作栏主页按钮为PopupMenu的锚视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能有Home键触发的PopupMenu?下面code是行不通的,因为动作视图尚未成立。

Is it possible to have Home button to trigger a PopupMenu? The following code would not work since the action view has not been set up.

public boolean onOptionsItemSelected(MenuItem item){
  switch (item.getItemId()) {
case android.R.id.home:
       PopupMenu popup = new PopupMenu(JournalList.this, item.getActionView());
       popup.inflate(R.menu.list_select_menu);
   popup.show();
}

家按钮不可用要访问此下列方式要么,则findItem未能找到菜单项:

The home button is not available to be accessed this following way either, the findItem fails to find the menu item:

public boolean onCreateOptionsMenu(Menu menu) {
   Button img = new Button(this);
   img.setText("Pop");
   menu.findItem(android.R.id.home).setActionView(someImageButton);
}

什么动作条Home键的一部分,所以可以作为锚视图或是否有建立视图第一种方式?

So what part of ActionBar Home button can be used as an anchor view or if there's a way to set up the view first?

感谢。

推荐答案

您可以在0像素上方最左边的锚视图添加到你的根ViewGroup中,

You can add a 0px top-left most anchor view to your root viewgroup,

和使用该视图的弹出菜单锚视图。

and use this view as the PopupMenu anchor view.

我觉得这是最简单的方式。

I think this is the most simple way.

这篇关于使用操作栏主页按钮为PopupMenu的锚视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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