【问题标题】:Insert a one or more new TableRow between two TableRow在两个 TableRow 之间插入一个或多个新的 TableRow
【发布时间】:2012-06-18 11:56:57
【问题描述】:

我正在开发一个 Android 应用程序。一个 Activity 有一个像这样的 TableLayout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ereportfillscroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">

        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TableLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/EReportTableLayout"
            android:layout_margin="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:shrinkColumns="*"  
            android:stretchColumns="*">

            <TableRow>
                <TextView
                    android:id="@+id/txtGenInfo"
                    android:layout_span="8"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/layout_title_general_information"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
            </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/txtPONo"
                android:layout_weight=".125"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="@string/layout_po_no"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>

        <!-- MORE ROWS -->

    </TableLayout>
        <Button
            android:id="@+id/btnPhoto"
            android:onClick="onTakeAPhotoClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Take a photo" />

    </LinearLayout>
</ScrollView>

例如,我需要以编程方式在第一行和第二行之间插入另一个 TableRow。我该怎么做=

【问题讨论】:

    标签: android layout tablelayout


    【解决方案1】:

    你可以这样做:

    TableLayout tl = (TableLayout) findViewById(R.id.EReportTableLayout);
    tl.addView(tableRowToInsert, index);
    

    【讨论】:

      【解决方案2】:

      可以试试addView(View child, int index)

       TableLayout tl = (TableLayout)findViewById(R.id.EReportTableLayout);
                     /* Create a new row to be added. */
                     TableRow tr = new TableRow(this);
                     tr.setLayoutParams(new LayoutParams(
                                    LayoutParams.FILL_PARENT,
                                    LayoutParams.WRAP_CONTENT));
                          /* Create a Button to be the row-content. */
                          TextView textView = new TextView(this);
                         textView .setText("txt");
                         textView .setID(R.id.txtPONo_2);
      
                          textView .setLayoutParams(new LayoutParams(
                                    LayoutParams.FILL_PARENT,
                                    LayoutParams.WRAP_CONTENT));
      
                          tr.addView(textView );
                    /* Add row to TableLayout. */
                        tl.addView (tr, index);
      

      【讨论】:

      • 这会将行添加到表中,但最后不在我要插入的行之间。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      相关资源
      最近更新 更多