动态创建的android TableLayout中不显示内容[英] Content not showing in dynamically created android TableLayout

本文是小编为大家收集整理的关于动态创建的android TableLayout中不显示内容的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试制作一个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,我看不到任何东西.任何想法为什么会发生这种情况?

推荐答案

您的TextView s是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);
//...

其他推荐答案

如下:

<?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);
  }

本文地址:https://www.itbaoku.cn/post/102639.html

问题描述

I'm trying to make a 3-column TableLayout but I never see anything. Here's my java code:

         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));

And here is my XML file:

<?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>

I get no error messages, I get nothing. The problem is that apart from the 2 Buttons which are out of the TableLayout, I can't see anything. Any idea why this happens?

推荐答案

Your TextViews are children of a TableRow so you should set an instance of TableRow.LayoutParams for the LayoutParams instead of the simple one(probably from the ViewGroup super class) that you currently use:

//...
        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);
//...

其他推荐答案

Try like this:

<?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);
  }