我已经使用TableLayout来显示数据。数据为json格式,因此我创建了具有填充数据的动态TableRow和TextView并将其放入TableLayout中。在测试应用程序中,我采用了一个修复字符串数组来代替json来填充表格。没关系

问题是所有代码都能正常运行而不会出错。甚至我都调试了每一行代码。我没有发现任何错误。如果发生事件,则我的数据未显示在表中。 TableLayout与开始时一样保持空白。
我的代码是:

    @Override
public void onCreate(Bundle savedInstanceState) {
    try
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabular_layout);
        final TableLayout tl = (TableLayout)findViewById(R.id.tblLyt);

        for(int i = 0; i<names.length; i++)
        {

            TableRow tr = new TableRow(this);
            TextView tv1 = new TextView(this);
            TextView tv2 = new TextView(this);

            createView(tr, tv1, Integer.toString(i+1));
            createView(tr, tv2, names[i]);

            tl.addView(tr);
        }

    }
    catch(Exception ex)
    {
        MessageBox(ex.getMessage());
    }
}

public void createView(TableRow tr, TextView t, String viewdata) throws Exception{
    try
    {
        t.setText(viewdata);
        t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        t.setTextColor(Color.DKGRAY);
        t.setBackgroundColor(Color.WHITE);
        t.setPadding(20, 0, 0, 0);
        tr.setPadding(0, 1, 0, 1);
        tr.addView(t); // add TextView to row.
    }
    catch(Exception ex)
    {
        throw ex;
    }
}


这是我的tabular_layout.xml文件。

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

   </LinearLayout>


我仍然是Android的新手。我可能犯了愚蠢的错误。但是我为此做了很多尝试,并进行了搜索。
所以救救我!

谢谢。

最佳答案

问题在下一行。将其阻止并运行您的程序

t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

关于android - 在表格布局中显示数据时出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6675030/

10-13 08:17
查看更多