Android 在为 MenuItem 的 ActionView 填充布局时,我应该为 root 传递什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23846146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What should I pass for root when inflating a layout to use for a MenuItem's ActionView?
提问by dlf
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
我附加到 aMenuItem
作为它的ActionView
(该项目出现在 中ActionBar
)。此视图的布局来自 XML。我是这样膨胀的:
ImageView actionView = (ImageView) layoutInflater.inflate(
R.layout.action_view_layout, null);
This appears to work fine. However; passing null for root
in the call to inflate()
makes Lint yell at me:
这似乎工作正常。然而; root
在调用中传递 null forinflate()
使 Lint 对我大喊大叫:
Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)
避免将 null 作为视图根传递(需要在膨胀布局的根元素上解析布局参数)
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 answersays 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?
在我的特定情况下,我似乎可以在没有 root 的情况下进行管理,但我希望代码尽可能正确。问题是,我不确定这里View
应该使用哪个作为根。这个答案说它应该是“围绕你想要膨胀的视图对象的小部件”。但这在这里意味着什么?用于操作栏的那个?活动?完全是别的什么?
Update: Reading the answers has made me suspect me the right thing to do is:
更新:阅读答案让我怀疑我正确的做法是:
- Get the
ActionBar
View
corresponding to theMenuItem
- Get its root
- Cast the root to a
ViewGroup
- Pass the result to the inflater
- 得到
ActionBar
View
对应的MenuItem
- 得到它的根
- 将根强制转换为
ViewGroup
- 将结果传递给充气机
This seems to work. Can anyone confirm or deny whether this is what should be done?
这似乎有效。任何人都可以确认或否认这是应该做的吗?
采纳答案by Damian Petla
I would simply do it like this:
我会简单地这样做:
menuItem.setActionView(R.layout.action_view_layout);
Let Android inflate the view for you.
让 Android 为您扩充视图。
If you need to do some extra changes on this ImageView call
如果您需要对此 ImageView 调用进行一些额外的更改
ImageView imageView = (ImageView) menuItem.getActionView();
Update
更新
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;
}
回答by Top Cat
You have look on this. it nicely explains the Layout Inflator as well.
你看看这个。它也很好地解释了 Layout Inflator。
There are two usable versions of the inflate()
method for a standard application:
inflate()
对于标准应用程序,该方法有两个可用版本:
inflate(int resource, ViewGroup root)
inflate(int resource, ViewGroup root, boolean attachToRoot)
The first parameter points to the layout resource you want to inflate. The second parameter is the root view of the hierarchy you are inflating the resource to attach to. When the third parameter is present, it governs whether or not the inflated view is attached to the supplied root after inflation.
第一个参数指向要膨胀的布局资源。第二个参数是您要扩充要附加到的资源的层次结构的根视图。当第三个参数存在时,它控制膨胀后的视图是否附加到提供的根。
It is these last two parameters that can cause a bit of confusion. With the two parameter version of this method, LayoutInflater will automatically attempt to attach the inflated view to the supplied root. However, the framework has a check in place that if you pass null for the root it bypasses this attempt to avoid an application crash.
正是这最后两个参数可能会引起一些混乱。使用此方法的两个参数版本,LayoutInflater 将自动尝试将膨胀的视图附加到提供的根。但是,框架有一个检查,如果您为根传递 null,它会绕过此尝试以避免应用程序崩溃。
Many developers take this behavior to mean that the proper way to disable attachment on inflation is by passing null as root; in many cases not even realizing that the three parameter version of inflate() exists.
许多开发人员认为这种行为意味着禁用通货膨胀附件的正确方法是将 null 作为 root 传递;在许多情况下,甚至没有意识到 inflate() 的三参数版本存在。
回答by user2399268
You generally want to pass whatever (ViewGroup sub-class) you're going to be adding actionView to in to inflate. in order to get actionView back from the inflate call and not the parent you'll want to add a 3rd parameter, false, so that it won't add the inflated view to the parent.
您通常希望传递任何(ViewGroup 子类)您要添加 actionView 以进行膨胀。为了从膨胀调用中获取 actionView 而不是父视图,您需要添加第三个参数 false,这样它就不会将膨胀视图添加到父视图。
ImageView actionView =
(ImageView)layoutInflater.inflate(R.layout.action_view_layout, parent, false);
// .. do whatever you like with actionView and then add it to it's parent
menuItem.addActionView(actionView)
There's a pretty good tutorial herethat goes about things a little differently. It's specifying action_view_layout
as part of menu.xml
with something like:
这里有一个非常好的教程,它的处理方式略有不同。它指定action_view_layout
为以下内容的一部分menu.xml
:
android:actionLayout="@layout/action_view_layout"
That may also work for you provided you're always using the same layout. if you go that route you'd be able to get the ActionView
by doing
如果您始终使用相同的布局,这也可能对您有用。如果你走那条路,你就可以ActionView
通过做
ImageView actionView = menu.findItem(R.id.whatever).getActionView();