Java 如何动态膨胀布局?
我在XML中定义了以下布局:Java 如何动态膨胀布局?,java,android,eclipse,android-layout,layout-inflater,Java,Android,Eclipse,Android Layout,Layout Inflater,我在XML中定义了以下布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/streamRelativeLayo
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/streamRelativeLayout">
<ListView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/streamListView"></ListView>
<ProgressBar android:layout_centerInParent="true" android:layout_height="wrap_content" android:id="@+id/streamProgressBar" android:layout_width="wrap_content"></ProgressBar>
</RelativeLayout>
如何使用LayoutFlater获取ListView和ProgressBar并在代码中分配它?您只需要活动引用并调用:
RelativeLayout relativeLayout = (RelativeLayout)activity.getLayoutInflater().inflate(R.layout.your_layout, null);
然后您可以使用它们的ID获取这两个视图
relativeLayout.findViewById(R.id.anId);
这样:
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 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);
现在,您可以通过
convertView.findViewById
或者,如果有很多实例,可以定义一个viewholder
static class CalViewHolder {
ListView con_list;
Progressbar con_progress;
}
holder = new CalViewHolder();
convertView.setTag(holder);
我拥有动态创建的LinarLayout,即LinearLayout ll=新的LinearLayout(上下文);此布局包含一些子视图,如framelayout、webview。现在,我如何扩展此linearlayout。?避免将null作为视图根传递(需要解析扩展布局根元素上的布局参数),因为在扩展布局时,避免将null作为父视图传递,否则将忽略扩展布局根上的任何布局参数。
static class CalViewHolder {
ListView con_list;
Progressbar con_progress;
}
holder = new CalViewHolder();
convertView.setTag(holder);