【问题标题】:Programmatically adding TableRow to TableLayout not working以编程方式将 TableRow 添加到 TableLayout 不起作用
【发布时间】:2011-11-08 22:04:23
【问题描述】:

我正在尝试按照代码here 以编程方式添加表格行

    /* Find Tablelayout defined in main.xml */
    TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines);
    /* Create a new row to be added. */
    TableRow tr = new TableRow(this);
    tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    /* Create a Button to be the row-content. */
    Button b = new Button(this);
    b.setText("Dynamic Button");
    b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    /* Add Button to row. */
    tr.addView(b);
    /* Add row to TableLayout. */
    //tr.setBackgroundResource(R.drawable.sf_gradient_03);
    tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

但是没有任何东西被绘制到屏幕上。 (和上面提到的here一样)

当我添加tr.setBackgroundResource(R.drawable.sf_gradient_03); 时,绘制的是带有背景图像的行,而不是按钮

这是我的布局

<!-- Sale Order Lines START -->
<LinearLayout android:id="@+id/SaleOrderLinesArea" android:orientation="vertical"
     android:layout_width="fill_parent" android:layout_height="fill_parent"
     android:padding="10dip" android:background="@drawable/sf_gradient_11" >
    <LinearLayout android:id="@+id/subHeaderArea" android:padding="10dip"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
        >
        <TextView android:id="@+id/screenTitle"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:textColor="@color/title_sub" android:textStyle="bold"
            android:text="Order Lines" />
    </LinearLayout>
    <TableLayout android:id="@+id/SaleOrderLines"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:padding="10dip" android:stretchColumns="*">
        <TableRow
            android:background="@drawable/sf_gradient_13" android:padding="10dip"
            >
            <TextView android:id="@+id/order_ref_label"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:textColor="@color/fg_prime" android:text="bLA bLA" />
            <TextView android:id="@+id/product_label"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textStyle="bold" android:textColor="@color/fg_title"
                android:text="@string/product" />
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textStyle="bold" android:textColor="@color/fg_title"
                android:text="@string/product_quantity" />
        </TableRow>
        <TableRow android:background="@drawable/sf_gradient_03"
            android:paddingLeft="10dip" android:paddingRight="10dip"
            >
            <TextView android:id="@+id/order_ref_label"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:textColor="@color/fg_prime" android:text="Fooo" />
            <Spinner android:id="@+id/product_spinner"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:prompt="@string/customer_prompt">
            </Spinner>
            <EditText android:id="@+id/product_uom_qty"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:singleLine="true" android:fadingEdge="horizontal" /> 
        </TableRow>
    </TableLayout>
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:paddingTop="5dip" android:paddingBottom="5dip">
        <Button android:id="@+id/add_button" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Add Lines" />
    </LinearLayout>
</LinearLayout>
<!-- Sale Order Lines END -->

【问题讨论】:

  • 你完成tl.requestLayout();了吗?在其中添加行之后?

标签: android android-layout tablelayout


【解决方案1】:

就我而言,我将宽度从TableRow.LayoutParams.WRAP_CONTENT 更改为TableRow.LayoutParams.MATCH_PARENT,然后就可以了。

TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

【讨论】:

    【解决方案2】:

    请注意,您的代码可能是完美的,但您使用的类不正确。您可能使用过 import android.view.ViewGroup.LayoutParams ,其中正确的类可从以下导入语句中获得:

    导入 android.widget.TableRow.LayoutParams

    【讨论】:

      【解决方案3】:

      知道了,每个LayoutParams 都应该属于android.widget.TableRow.LayoutParams,但提供给tl.addView(...) 的除外

      /* Find Tablelayout defined in main.xml */
      TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines);
      /* Create a new row to be added. */
      TableRow tr = new TableRow(this);
      tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
      /* Create a Button to be the row-content. */
      Button b = new Button(this);
      b.setText("Dynamic Button");
      b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
      /* Add Button to row. */
      tr.addView(b);
      /* Add row to TableLayout. */
      //tr.setBackgroundResource(R.drawable.sf_gradient_03);
      tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
      

      【讨论】:

      • 效果很好,谢谢。 但是...在我的例子中,我有几百行要添加,每行有 5 个 TextView 列。它最终花费了大约 7 秒来创建自己。相反,我发现一个大小一致的 LinearLayout 的 ListView 仅在 1 秒内加载。该解决方案在post by EzzyLearning 中进行了描述
      • 谢谢!有效。但我不明白“提供给 tl.addView() 的除外”,因为您在那里也使用过它
      • 在网格中添加一条线似乎需要做很多工作。新的构造函数被调用了 4 次。
      • @EricBarr 链接现在重定向到垃圾邮件。
      猜你喜欢
      • 2012-12-17
      • 1970-01-01
      • 2012-12-29
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多