内容没有显示在动态创建的机器人TableLayout [英] Content not showing in dynamically created android TableLayout

查看:104
本文介绍了内容没有显示在动态创建的机器人TableLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个3列 TableLayout ,但我从来没有看到任何东西。这是我的java code:

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

这里是我的XML文件:

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>

我没有得到任何的错误信息,我什么也没得到。问题是,除了2 按钮这是出了 TableLayout ,我什么都看不到。任何想法,为什么出现这种情况?

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?

推荐答案

的TextView 是一个的TableRow 所以你应该而不是的LayoutParams 从设置 TableRow.LayoutParams 一个实例简单的(可能是的ViewGroup 超类),您当前使用的:

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

这篇关于内容没有显示在动态创建的机器人TableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆