如何使用布局膨胀一个视图

我有一个用XML定义的布局。 它还包含:

<RelativeLayout android:id="@+id/item" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 

我想膨胀这个RelativeView与其他XML布局文件。 根据情况我可以使用不同的布局。 我应该怎么做? 我正在尝试不同的变化

 RelativeLayout item = (RelativeLayout) findViewById(R.id.item); item.inflate(...) 

但是没有一个能正常工作。

我不知道我有没有跟着你的问题 – 你是否试图将一个子视图添加到RelativeLayout? 如果是这样的话,你想要做的事情是:

 RelativeLayout item = (RelativeLayout)findViewById(R.id.item); View child = getLayoutInflater().inflate(R.layout.child, null); item.addView(child); 

您膨胀一个XML资源。 请参阅LayoutInflater文档 。

如果您的布局位于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); 

虽然答案迟了,但是想补充一点,就是这样做

 LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = layoutInflater.inflate(R.layout.mylayout, item ); 

其中项目是您要添加子布局的父布局。

尽pipe它是一个旧的post,但是如果将从xml中膨胀的子视图添加到视图组布局中,您需要调用inflate并且知道正在处理的视图组types被添加到。 喜欢:

 View child = getLayoutInflater().inflate(R.layout.child, item, false); 

膨胀方法相当重载,并在文档中描述了这部分用法。 我有一个问题,从xml单个视图膨胀不正确的父alignment,直到我做了这种types的变化。

更简单的方法是使用

 View child = View.inflate(context, R.layout.child, null) item.addChild(child) //attach to your item 

布局通胀

 View view = null; LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.mylayout, null); main.addView(view); 

如果您不在活动中,则可以使用LayoutInflater类的静态from()方法来获取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); 

(我知道这已经差不多4年前了,但还是值得一提的)

检查这个链接。 它有答案 – > https://possiblemobile.com/2013/05/layout-inflation-as-intended/

由于我独特的情况,我遇到了这个错误最困难的时刻,但终于find了解决办法。

我的情况:我正在使用一个单独的视图(XML),它包含一个WebView ,然后在单击主活动视图中的一个button时在AlertDialog打开。 但不知何故, WebView属于主要活动视图(可能是因为我从这里拉的资源),所以就在我把它分配给我的AlertDialog (作为一个视图)之前,我必须得到我的WebView的父母,把它到ViewGroup ,然后删除该ViewGroup上的所有视图。 这工作,我的错误消失了。

 // set up Alert Dialog box AlertDialog.Builder alert = new AlertDialog.Builder(this); // inflate other xml where WebView is LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE); View v = layoutInflater.inflate(R.layout.your_webview_layout, null); final WebView webView = (WebView) v.findViewById(R.id.your_webview_id); // more code... 

….后来我加载了我的WebView ….

 // first, remove the parent of WebView from it's old parent so can be assigned a new one. ViewGroup vg = (ViewGroup) webView.getParent(); vg.removeAllViews(); // put WebView in Dialog box alert.setView(webView); alert.show(); 

如果你想多次添加单个视图,那么你必须使用

  layoutInflaterForButton = getActivity().getLayoutInflater(); for (int noOfButton = 0; noOfButton < 5; noOfButton++) { FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null); btnContainer.addView(btnView); } 

如果你喜欢

  layoutInflaterForButton = getActivity().getLayoutInflater(); FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null); 

 for (int noOfButton = 0; noOfButton < 5; noOfButton++) { btnContainer.addView(btnView); } 

那么它会抛出所有就绪添加视图的exception。

我已经使用下面的代码片段,它为我工作。

 LinearLayout linearLayout = (LinearLayout)findViewById(R.id.item); View child = getLayoutInflater().inflate(R.layout.child, null); linearLayout.addView(child);