【问题标题】:Content not showing in dynamically created android TableLayout动态创建的 android TableLayout 中未显示内容
【发布时间】:2012-04-26 15:24:48
【问题描述】:

我正在尝试制作一个 3 列的 TableLayout,但我什么也没看到。这是我的java代码:

         TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
         TableRow tr = new TableRow(this);
         tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));


         TextView tv = new TextView(this);
         tv.setLayoutParams(new LayoutParams(
                   LayoutParams.FILL_PARENT,
                   LayoutParams.WRAP_CONTENT));
         tv.setText(data[0]);
         tr.addView(tv);

         TextView tv1 = new TextView(this);
         tv1.setLayoutParams(new LayoutParams(
                 LayoutParams.FILL_PARENT,
                 LayoutParams.WRAP_CONTENT));
         tv1.setText(data[1]);
         tr.addView(tv1);

         TextView tv2 = new TextView(this);
         tv2.setLayoutParams(new LayoutParams(
               LayoutParams.FILL_PARENT,
               LayoutParams.WRAP_CONTENT));
         tv2.setText(data[2]);
         tr.addView(tv2);

        /* Add row to TableLayout. */
        tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/LinearLayout1" >



<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="350dp"
    android:layout_marginTop="10dp"
    >

</TableLayout>


<Button
    android:id="@+id/BorrarPedido"
    android:layout_width="204dp"
    android:layout_height="wrap_content"
    android:text="Borrar Pedido"
    android:layout_gravity="center" />

<Button
    android:id="@+id/EnviarPedido"
    android:layout_width="204dp"
    android:layout_height="wrap_content"
    android:text="Enviar Pedido"
    android:layout_gravity="center" />

   </LinearLayout>

我没有收到任何错误消息,我什么也没有收到。问题是除了TableLayout 之外的2 个Buttons,我什么都看不到。知道为什么会这样吗?

【问题讨论】:

  • 数据是否具有值。也可以尝试设置 TextViews 的 setTextColor 。有时您的活动主题可能会导致混乱
  • 数据肯定有值。我尝试更改 textview 的颜色,但视图上仍然没有任何内容。

标签: android android-layout tablelayout


【解决方案1】:

您的TextViews 是TableRow 的子代,因此您应该为LayoutParams 设置TableRow.LayoutParams 的实例,而不是您当前使用的简单实例(可能来自ViewGroup 超类):

//...
        TextView tv = new TextView(this);
        tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
        tv.setText("text 1");
        tr.addView(tv);

        TextView tv1 = new TextView(this);
        tv1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
        tv1.setText("text 2");
        tr.addView(tv1);

        TextView tv2 = new TextView(this);
        tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
        tv2.setText("text 3");
        tr.addView(tv2);
//...

【讨论】:

  • 你刚刚拯救了我的理智 +1。
【解决方案2】:

试试这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="350dp"
        android:layout_marginTop="10dp" android:stretchColumns="*" >
    </TableLayout>

    <Button
        android:id="@+id/BorrarPedido"
        android:layout_width="204dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Borrar Pedido" />

    <Button
        android:id="@+id/EnviarPedido"
        android:layout_width="204dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Enviar Pedido" />
</LinearLayout>


    TableLayout tblLayout = (TableLayout)findViewById(R.id.tableLayout1);

    TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);

    TableRow row = new TableRow(this);
    row.setBackgroundColor(Color.DKGRAY);
    tblLayout.addView(row, layoutParams);

    addToTableRow("Name", row, Color.WHITE);
    addToTableRow("Age", row, Color.WHITE);
    addToTableRow("Location", row, Color.WHITE);
  }

  private void addToTableRow(String str, TableRow row,int color) 
  {
    TextView t = new TextView(this);
    t.setTextColor(color);
    t.setText(str);
    row.addView(t);
  }

【讨论】:

  • android:orientation 这么重要吗?
猜你喜欢
  • 2013-06-25
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多