android - 在表格布局中显示数据的问题

标签 android

我使用了 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/

相关文章:

android - 如何在 Android 应用程序中打开 Youtube 视频链接?

Android:作为市场应用程序的本地服务?

java - 该应用程序可能在其主线程中执行了过多的工作

java - 通过整数引用使用 ScaleType 枚举值

java - 在Android Studio中运行我的应用程序时出现DuplicatedError

android - 控制 MDM/MAM 受控设备的用户可以下载哪些应用程序

android - android 中的 UnknownHostException

android - 如何返回关闭 Activity 之间的 Activity ?

java - 我可以在 android studio 中使用过时版本的 SDK 构建工具吗?

android - 膨胀Relativelayout Android