我是新手,并且难以实现所需的输出。
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代码:

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);}




请告诉我我需要对我当前的代码做些什么更改为给定图像所需的产品

最佳答案

您可以像这样使用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>

09-05 08:21