在运行时android构建布局
|
我有一个复杂的xml布局,其中包含列表视图。.列表视图中的一行包含几个文本字段,它们均匀分布。我正在使用textview来存储文本,然后最终将所有项目添加到该行中……它的工作非常正常。
但是现在我有这样的情况,在不确定的情况下,我可能会从Web服务获取多少文本字段。因此,我需要在运行时动态创建textview,填充它们,然后将其插入列表。
无论如何在运行时声明,添加和填充新的textview字段?
还是有实现两个字段之间的间距?
首次通话的结果
__________________________
|_____|_____|_____|______|
第二次通话的结果
________________________________
|_____|_____|_____|______|______|
我尝试实现下面(肯尼)提供的解决方案,但是由于某种原因,我无法将视图添加到列表中。.下面是我的代码
public class HeaderAdapter extends ArrayAdapter<Header> {
final Header[] listSymbols;
private TextView textView;
private LinearLayout row;
public HeaderAdapter(Context context, int symResourceID,
Header[] objects) {
super(context, symResourceID, objects);
listSymbols = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.header_view, parent, false);
Header headerRec = listSymbols[position];
for(int i = 0 ; i < listSymbols.length;i++){
textView = new TextView(getContext());
textView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view
ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view
textView.setId(i);
row.add??
}
}
称为此的主要活动
setContentView(R.layout.main);
headerList.add(new Header(\"Symbol\",\"Quantity\",\"Price\",\"Price Yesterday\",\"52 Week High\",\"52 Week Low\",\"Change\",\"Days Gain\",\"Days Gain %\",\"Returns\"));
Header[] tmpHeaderList = headerList.toArray(new Header[headerList.size()]);
ArrayAdapter<Header> headerAdapter = new HeaderAdapter(this,R.layout.twoway_header_view,tmpHeaderList);
headerListView.setAdapter(headerAdapter);
xml布局文件..主文件
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"fill_parent\" android:layout_height=\"fill_parent\"
android:orientation=\"vertical\">
<HorizontalScrollView android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\" android:scrollbars=\"none\"
android:id=\"@+id/headerHv\">
<ListView android:id=\"@+id/header_listView1\"
android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\"
android:background=\"@color/white\" android:cacheColorHint=\"#00000000\"
android:smoothScrollbar=\"true\" android:scrollbars=\"none\" />
</HorizontalScrollView>
</LinearLayout>
定义行模板的文件
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"
android:orientation=\"horizontal\"
>
<TextView android:id=\"@+id/headerList\" android:layout_width=\"80dp\"
android:layout_height=\"wrap_content\" android:textColor=\"#000000\"
android:typeface=\"sans\" android:textStyle=\"normal\" />
</LinearLayout>
没有找到相关结果
已邀请:
2 个回复
晤默报
因此,您可以尝试:
诧不达