【问题标题】:Adding Rows to Table Layout Programmatically以编程方式将行添加到表格布局
【发布时间】:2023-03-15 06:48:01
【问题描述】:

嗨,我想在活动第一次开始时获得一个表格布局来填充 XML 的数据,但我是一个新手,所以如果有人知道我该怎么做,我真的不知道该怎么做这个

【问题讨论】:

    标签: android tablelayout


    【解决方案1】:

    您可以动态添加表格行

    for(int i=0;i<youxmlArray.size();i++){
        // get a reference for the TableLayout
        TableLayout table = (TableLayout)findViewById(R.id.TableLayout01);
        // create a new TableRow
        TableRow row = new TableRow(this);
        // create a new TextView for showing xml data
        TextView t = new TextView(this);
        // set the text to "text xx"
        t.setText( "YOUR XML DATA  HERE");
        // add the TextView  to the new TableRow
        row.addView(t);
        // add the TableRow to the TableLayout
        table.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    }
    

    并在您的 xml 布局中添加一个 TableLayout 作为

    <TableLayout 
    android:id = "@+id/TableLayout01" 
    android:layout_width = "fill_parent" 
    android:layout_height = "wrap_content"   
    android:stretchColumns = "0" >
       < TableRow android:id = "@+id/TableRow01" 
       android:layout_width = "wrap_content" 
       android:layout_height = "wrap_content" >
          < TextView android:id = "@+id/TextView01" 
            android:layout_width = "fill_parent" 
            android:layout_height = "wrap_content" 
            android:text = "textfield 1-1" >
            </ TextView >
       </ TableRow >
    </ TableLayout > 
    

    【讨论】:

    • 它会遍历所有的 XML 数据直到到达最后一个条目吗?
    • 如果您有任何问题,请尝试然后发布您的代码,我将对其进行编辑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多