Android 如何使用布局膨胀一个视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2335813/
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
How to inflate one view with a layout
提问by Michal Dymel
I have a layout defined in XML. It contains also:
我有一个用 XML 定义的布局。它还包含:
<RelativeLayout
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
I would like to inflate this RelativeView with other XML layout file. I may use different layouts depending on a situation. How should I do it? I was trying different variations of
我想用其他 XML 布局文件来扩充这个 RelativeView。我可能会根据情况使用不同的布局。我该怎么做?我正在尝试不同的变体
RelativeLayout item = (RelativeLayout) findViewById(R.id.item);
item.inflate(...)
But none of them worked fine.
但没有一个工作正常。
回答by Graeme Duncan
I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
我不确定我是否遵循了您的问题 - 您是否尝试将子视图附加到 RelativeLayout?如果是这样,您想按照以下方式做一些事情:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
回答by ccheneson
You inflate an XML resource. See the LayoutInflater doc.
您膨胀了一个 XML 资源。请参阅LayoutInflater 文档。
If your layout is in a mylayout.xml, you would do something like:
如果您的布局在mylayout.xml 中,您将执行以下操作:
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.item);
回答by Shaista Naaz
Though late answer, but would like to add that one way to get this
虽然回答晚了,但想添加一种方法来获得这个
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.mylayout, item );
where item
is the parent layout where you want to add a child layout.
这里item
是你想添加一个子布局的父布局。
回答by Maximus
It's helpful to add to this, even though it's an old post, that if the child view that is being inflated from xml is to be added to a viewgroup layout, you need to call inflate with a clue of what type of viewgroup it is going to be added to. Like:
添加到这一点很有帮助,即使它是一个旧帖子,如果将从 xml 膨胀的子视图添加到视图组布局,您需要调用 inflate 并提供它正在使用的视图组类型的线索要添加到。喜欢:
View child = getLayoutInflater().inflate(R.layout.child, item, false);
The inflate method is quite overloaded and describes this part of the usage in the docs. I had a problem where a single view inflated from xml wasn't aligning in the parent properly until I made this type of change.
inflate 方法非常重载,并在文档中描述了这部分用法。我遇到了一个问题,在我进行此类更改之前,从 xml 膨胀的单个视图在父级中没有正确对齐。
回答by Ashish Rawat
Even simpler way is to use
更简单的方法是使用
View child = View.inflate(context, R.layout.child, null)
item.addChild(child) //attach to your item
回答by Happy Singh
layout inflation
布局通胀
View view = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
main.addView(view);
回答by Juan Cortés
If you're not in an activity you can use the static from()
method from the LayoutInflater
class to get a LayoutInflater
, or request the service from the context method getSystemService()
too :
如果您不在活动中,则可以使用类中的静态from()
方法LayoutInflater
来获取LayoutInflater
,或者也可以从上下文方法中请求服务getSystemService()
:
LayoutInflater i;
Context x; //Assuming here that x is a valid context, not null
i = (LayoutInflater) x.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//OR
i = LayoutInflater.from(x);
(I know it's almost 4 years ago but still worth mentioning)
(我知道这已经是 4 年前的事了,但仍然值得一提)
回答by eSparkBiz Team
AttachToRoot Set to True
AttachToRoot 设置为 True
Just think we specified a button in an XML layout file with its layout width and layout height set to match_parent.
想想我们在 XML 布局文件中指定了一个按钮,其布局宽度和布局高度设置为 match_parent。
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/custom_button">
</Button>
On This Buttons Click Event We Can Set Following Code to Inflate Layout on This Activity.
在此按钮上单击事件,我们可以设置以下代码来扩展此活动的布局。
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.yourlayoutname, this);
Hope this solution works for you.!
希望这个解决方案对你有用。!
回答by Rohit Mhatre
Try this code :
试试这个代码:
- If you just want to inflate your layout :
- 如果你只是想膨胀你的布局:
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,null); // Code for inflating xml layout
RelativeLayout item = view.findViewById(R.id.item);
- If you want to inflate your layout in container(parent layout) :
- 如果你想在容器(父布局)中膨胀你的布局:
LinearLayout parent = findViewById(R.id.container); //parent layout.
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,parent,false);
RelativeLayout item = view.findViewById(R.id.item); //initialize layout & By this you can also perform any event.
parent.addView(view); //adding your inflated layout in parent layout.
回答by John smith
If you want to add a single view multiple time then you have to use
如果要多次添加单个视图,则必须使用
layoutInflaterForButton = getActivity().getLayoutInflater();
for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
btnContainer.addView(btnView);
}
If you do like
如果你喜欢
layoutInflaterForButton = getActivity().getLayoutInflater();
FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
and
和
for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
btnContainer.addView(btnView);
}
then it will throw exception of all ready added view.
然后它将抛出所有准备好的添加视图的异常。