在运行时android构建布局

时间:2011-06-23 21:36:44

标签: android android-layout android-listview android-linearlayout

我有一个复杂的xml布局,它有列表视图。列表视图中的一行包含几个均匀间隔的文本字段。我正在使用textview存储文本,然后最后将所有项目添加到行...它的工作完全正常。 但现在我有一个案例,我不确定,我可以从网络服务获得多少文本字段。因此我需要在运行时动态创建textview,填充它们然后插入列表.. 无论如何在运行时声明,添加和填充新的textview字段? 或者无论如何要实现两个字段之间的间距?

第一次通话的结果

__________________________
|_____|_____|_____|______| 

第二次通话的结果

________________________________
|_____|_____|_____|______|______|

我尝试实现下面提供的解决方案(Kenny),但由于某种原因我无法将视图添加到列表中..下面是我的代码

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 个答案:

答案 0 :(得分:4)

这是我从列表中动态生成自定义按钮的方式,你可以用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);            
    }

答案 1 :(得分:0)

你可以在代码中完成。在循环中声明TextView并使用RelativeLayout将它们相互定位。

相关问题