如何在表格布局中放置动态表格行并使用Android在表格行中动态放置Textview?

时间:2013-11-18 08:04:46

标签: java android

我是新手并且难以实现我所需的输出。 XML代码:  //此代码在ScrollView内部

 <LinearLayout 
        android:id="@+id/statusSecond_Layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
         <TableLayout 
             android:id="@+id/statusDisciplineTable_layout"
             android:layout_height="wrap_content"
             android:layout_width="fill_parent"

             ></TableLayout>

    </LinearLayout>

JAVA CODE:

setContentView(R.layout.status_view);
statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
for(int i=0;i<2;i++)
{
    TableRow statusTableRow = new TableRow(this);
    statusTableRow.setId(i);
    statusTableRow.setOrientation(TableRow.VERTICAL);
    TextView productsTextView = new TextView(this);
    productsTextView.setText("product name:"+i);
    statusTableRow.addView(productsTextView);
    //statusTableRow.setBackgroundColor(Color.BLACK);


    for(int j=0;j<2;j++)
    {
        RelativeLayout statusRelativelayout = new RelativeLayout(this);
        TableRow.LayoutParams rlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        rl.setMargins(0, 0, 16, 0);
        rl.addRule(RelativeLayout.ALIGN_LEFT);
            TextView label = new TextView(this);
        label.setId(j);
        label.setText("abcd:"+j);
        label.setLayoutParams(rl);
    statusRelativelayout.addView(label);
    statusTableRow.addView(statusRelativelayout,rlp);   

    }
    statusTableLayout.addView(statusTableRow);}

code output

请告诉我,我需要将当前代码更改为产品所需的图像 required outout

1 个答案:

答案 0 :(得分:1)

您可以使用LinearLayout代替此TableRow

TableLayout statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
        for(int i=0;i<2;i++)
        {
            LinearLayout statusTableRow = new LinearLayout(this);
            statusTableRow.setId(i);
            statusTableRow.setOrientation(LinearLayout.VERTICAL);
            TextView productsTextView = new TextView(this);
            productsTextView.setText("product name:"+i);
            statusTableRow.addView(productsTextView);
            //statusTableRow.setBackgroundColor(Color.BLACK);


            for(int j=0;j<2;j++)
            {
                RelativeLayout statusRelativelayout = new RelativeLayout(this);

                RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
                rl.setMargins(0, 0, 16, 0);
                rl.addRule(RelativeLayout.ALIGN_LEFT);
                    TextView label = new TextView(this);
                label.setId(j);
                label.setText("abcd:"+j);
                label.setLayoutParams(rl);
            statusRelativelayout.addView(label);
            statusTableRow.addView(statusRelativelayout);   

            }
            statusTableLayout.addView(statusTableRow);}

还将所有内容都放在Relativelayout内,然后将ScrollView放在这里

<?xml version="1.0" encoding="utf-8"?>
<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
<LinearLayout 
        android:id="@+id/statusSecond_Layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
         <TableLayout 
             android:id="@+id/statusDisciplineTable_layout"
             android:layout_height="wrap_content"
             android:layout_width="fill_parent"

             >

             <LinearLayout
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content" >
             </LinearLayout>

         </TableLayout>

    </LinearLayout>

</RelativeLayout>

</ScrollView>