【问题标题】:Problems with dynamically adding TableRow in TableLayout在 TableLayout 中动态添加 TableRow 的问题
【发布时间】:2012-03-26 20:28:33
【问题描述】:

我的代码:

public class Test extends Activity {

private TableLayout tableLayout = null;
private Button add = null;

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);

    setContentView(R.layout.main);

    tableLayout = (TableLayout)findViewById(R.id.tableLayout);

    add = (Button)findViewById(R.id.agregar);
    add.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            TableRow fila = new TableRow(Test.this);
            Button eliminar = new Button(Test.this);
            eliminar.setText("delete");
            eliminar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            fila.addView(eliminar,new TableLayout.LayoutParams(
                      LayoutParams.WRAP_CONTENT,
                      LayoutParams.WRAP_CONTENT));
            tableLayout.addView(fila,new TableLayout.LayoutParams(
                      LayoutParams.MATCH_PARENT,
                      LayoutParams.WRAP_CONTENT));
        }
    });
}

我希望每次点击 add Button 时它都会添加一个新行。我的xml如下:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:id="@+id/relativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:text="Column 1"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <Button
                android:id="@+id/button1"
                android:text="Delete" />
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/agregar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tableLayout"
        android:text="Add" />
</RelativeLayout>

每次我点击Button,它什么都不做。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 如果您找到了问题的解决方案,请为您的问题添加答案并接受,以便问题得到解答。
  • 如何使用 FOR LOOP 在 tablerow 中添加按钮,然后对于每个按钮都会有另一个 TABLEROW 将保持三个线性布局水平方向有人可以帮我吗?

标签: android android-layout android-tablelayout


【解决方案1】:

(在问题编辑中回答。转换为社区 wiki 答案。参见What is the appropriate action when the answer to a question is added to the question itself?

OP 写道:

我发现他们不能是LayoutParams。如果它们是TableRow 的一部分,它们必须是TableRow.LayoutParams,如果它们在TableLayout 内,它们必须是TableLayout.LayoutParams。像下面这样:

eliminar.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            fila.addView(eliminar,new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            tableLayout.addView(fila,new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多