如何解决在运行时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>
解决方法
这是我从列表中动态生成自定义按钮的方式,您可以使用textViews执行相同的操作:
//Setup Buttons
layout = (LinearLayout)findViewById(R.id.layoutBars);
int count = lBars.size();
for(int i = 0; i< count;i++){
final Bar b = lBars.get(i);
BarButton button = new BarButton(DDTBars.this,R.drawable.barsmall,b.getName().toUpperCase());
button.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
button.setId(i);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Run activity passing name of the bar to retrieve data
Intent i = new Intent(DDTBars.this,DDTBar.class);
i.putExtra(\"name\",b.getName());
startActivity(i);
}
});
layout.addView(button);
}
因此,您可以尝试:
//Setup TextViews
layout = (LinearLayout)findViewById(R.id.mylayout);
int count = myTextList.size();
for(int i = 0; i< count;i++){
TextView txtView = new TextView(this);
txtView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,//Width of the view
ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view
txtView.setId(i);
layout.addView(txtView);
}
, 您可以用代码来完成。循环声明TextView \,并使用RelativeLayout将它们彼此相对放置。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。