我应该怎么传根充气布局要用于菜单项的时候的ActionView? [英] What should I pass for root when inflating a layout to use for a MenuItem's ActionView?

查看:255
本文介绍了我应该怎么传根充气布局要用于菜单项的时候的ActionView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ImageView 我附加到菜单项而ActionView (项目出现在动作条)。该视图的布局来自XML。我膨胀它像这样:

I have an ImageView that I attach to a MenuItem as its ActionView (the item appears in the ActionBar). The layout for this view comes from XML. I'm inflating it like so:

ImageView actionView = (ImageView) layoutInflater.inflate(
   R.layout.action_view_layout, null);

这似乎很好地工作。然而;传递null为呼叫根膨胀()使得皮棉冲我嚷嚷:

This appears to work fine. However; passing null for root in the call to inflate() makes Lint yell at me:

避免传递null作为视图根(需要解决布局
  虚增布局的根元素参数)

Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)

我貌似没有管理我的具体情况下根,但我宁愿有code尽可能正确的。问题是,我不知道该查看应作为这里的根源。 这个答案说,这应该是是围绕要抬高视图对象的部件。但是,这是什么意思吗?在一个操作栏?活动?别的东西完全?

I can seemingly manage without a root in my specific case, but I'd rather have the code be as correct as possible. The problem is, I'm not sure which View should be used as the root here. This answer says it should be "the widget that is surrounding the view objects that you want to inflate." But what does that mean here? The one for the action bar? The activity? Something else entirely?

更新:阅读的答案使我怀疑我做正确的事情是:

Update: Reading the answers has made me suspect me the right thing to do is:


  1. 获得动作条 查看对应的菜单项

  2. 获取其根

  3. 铸造根到的ViewGroup

  4. 结果传递给充气

  1. Get the ActionBar View corresponding to the MenuItem
  2. Get its root
  3. Cast the root to a ViewGroup
  4. Pass the result to the inflater

这似乎工作。任何人都可以证实或否认这是否是应做什么?

This seems to work. Can anyone confirm or deny whether this is what should be done?

推荐答案

我只想做这样的:

menuItem.setActionView(R.layout.action_view_layout);

让Android的夸大你的看法。

Let Android inflate the view for you.

如果你需要做这个的ImageView调用一些额外的修改

If you need to do some extra changes on this ImageView call

ImageView imageView = (ImageView) menuItem.getActionView();

更新

为了迎合你的好奇心。这就是谷歌从乡亲引擎盖下做的:

In order to cater to your curiosity. That is what folks from Google do under the hood:

public MenuItem setActionView(int resId) {
    final Context context = mMenu.getContext();
    final LayoutInflater inflater = LayoutInflater.from(context);
    setActionView(inflater.inflate(resId, new LinearLayout(context), false));
    return this;
}

这篇关于我应该怎么传根充气布局要用于菜单项的时候的ActionView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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