Android – dynamic添加视图到视图

我有一个视图的布局 –

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="0px" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/items_header" style="@style/Home.ListHeader" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/items_none" android:visibility="gone" style="@style/TextBlock" android:paddingLeft="6px" /> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/items_list" /> </LinearLayout> 

我想做什么,是在我的主要活动,这样的布局

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="0px" android:id="@+id/item_wrapper"> </LinearLayout> 

我想遍历我的数据模型,并将包含第一个布局的多个视图注入到主布局中。 我知道我可以通过在代码中完全构build控件来做到这一点,但我想知道是否有办法dynamic构build视图,以便继续使用布局,而不是将所有内容都放在代码中。

使用LayoutInflater根据您的布局模板创build一个视图,然后将其注入到您需要的视图中。

 LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = vi.inflate(R.layout.your_layout, null); // fill in any details dynamically here TextView textView = (TextView) v.findViewById(R.id.a_text_view); textView.setText("your text"); // insert into main view ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point); insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 

您可能需要调整要插入视图的索引。

此外,根据您希望如何适合父视图来设置LayoutParams。 例如FILL_PARENTMATCH_PARENT等。

请参阅LayoutInflater类。

 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); ViewGroup parent = (ViewGroup)findViewById(R.id.where_you_want_to_insert); inflater.inflate(R.layout.the_child_view, parent); 

它看起来像你真正想要一个ListView与一个自定义适配器来膨胀指定的布局。 使用ArrayAdapter和方法notifyDataSetChanged()您可以完全控制Views的生成和呈现。

看看这些教程

为了使@Mark Fisher的答案更加清晰,插入的视图被充气应该是一个布局文件夹下的xml文件,但是没有像LinearLayout等布局(ViewGroup)。 我的例子:

RES /布局/ my_view.xml

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/i_am_id" android:text="my name" android:textSize="17sp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> 

然后,插入点应该是一个像LinearLayout的布局:

RES /布局/ activity_main.xml中

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/aaa" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/insert_point" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout> </RelativeLayout> 

那么代码应该是

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shopping_cart); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.my_view, null); ViewGroup main = (ViewGroup) findViewById(R.id.insert_point); main.addView(view, 0); } 

我发表这个非常相似的答案的原因是,当我试图实现马克的解决scheme时,我被困在什么XML文件,我应该使用insert_point和子视图。 我首先在子视图中使用了布局,并且完全不工作,这花了我几个小时才弄清楚。 所以希望我的探索可以节省别人的时间。

  // Parent layout LinearLayout parentLayout = (LinearLayout)findViewById(R.id.layout); // Layout inflater LayoutInflater layoutInflater = getLayoutInflater(); View view; for (int i = 1; i < 101; i++){ // Add the text layout to the parent layout view = layoutInflater.inflate(R.layout.text_layout, parentLayout, false); // In order to get the view we have to use the new view with text_layout in it TextView textView = (TextView)view.findViewById(R.id.text); textView.setText("Row " + i); // Add the text view to the parent layout parentLayout.addView(textView);