如何动态扩充布局?

我在XML中定义了以下布局:

     

如何使用LayoutInflater来获取ListView和ProgressBar并在代码中分配它?

通过这种方式:

 View v = getLayoutInflater().inflate(R.layout.YOUR_LAYOUT_ID, null); ListView listview = (ListView) v.findViewById(R.id.streamListView); ProgressBar progress = (ProgressBar) v.findViewById(R.id.streamProgressBar); 

您只需要活动参考并致电:

 RelativeLayout relativeLayout = (RelativeLayout)activity.getLayoutInflater().inflate(R.layout.your_layout, null); 

然后你可以使用他们的id获取两个视图

 relativeLayout.findViewById(R.id.anId); 

试试这个

 RelativeLayout myLayout = (RelativeLayout)getLayoutInflater().inflate( R.layout.you_xml_id, null); ListView list = (ListView)myLayout.findViewById(R.id.streamListView); ProgressBar bar = (ProgressBar)myLayout.findViewById(R.id.streamProgressBar); 
 private LayoutInflater mInflater; View convertView; mInflater = LayoutInflater.from(context); convertView = mInflater.inflate(R.xml.yourxmlname, null); 

现在你可以通过访问这些项目了

 convertView.findViewById 

或者如果你有很多实例,你可以定义一个视图

 static class CalViewHolder { ListView con_list; Progressbar con_progress; } holder = new CalViewHolder(); convertView.setTag(holder);