簡體   English   中英

如何使用Android將動態TableRow放置在TableLayout中並將Textview動態放置在TableRow中?

[英]how to put dynamic TableRow in TableLayout and place Textview dynamically in TableRow using 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代碼:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM