【问题标题】:Adding to Table Android TableLayout添加到表 Android TableLayout
【发布时间】:2014-12-09 07:51:12
【问题描述】:

我想通过将数据插入表来创建一个非常简单的调度程序。我得到这些信息没有问题,但是,我似乎无法插入到有意义的表中。通过有意义的表格,我的意思是这样开始的表格,随后在其受人尊敬的标题下方插入了后续添加:

第 1 天:

第 2 天:

第 3 天:

第 4 天:

我从用户那里获取课程信息,并希望根据课程在课程表中出现的时间对课程进行分类。我很难将其组织到表格中。

对我如何有效地排序有什么建议吗?

【问题讨论】:

    标签: android tablelayout


    【解决方案1】:

    使用TableLayout 并在 XML 中创建您的布局模板。例如:

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp" >
    
            <TextView
                android:id="@+id/textView1"
                android:text="DAY 1:"
                android:textAppearance="?android:attr/textAppearanceLarge" />
    
        </TableRow>
    
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp" >
    
            <TextView
                android:id="@+id/textView1"
                android:text="DAY 2:"
                android:textAppearance="?android:attr/textAppearanceLarge" />
    
        </TableRow>
    
        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp" >
    
            <TextView
                android:id="@+id/textView1"
                android:text="DAY 3:"
                android:textAppearance="?android:attr/textAppearanceLarge" />
    
        </TableRow>
    
        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp" >
    
            <TextView
                android:id="@+id/textView1"
                android:text="DAY 4:"
                android:textAppearance="?android:attr/textAppearanceLarge" />
    
        </TableRow>
    
    </TableLayout>
    

    然后检查课程信息列表,并以编程方式将有关它的信息作为TableRow 添加到您之前创建的布局中。为此使用TableLayout#addView(View child, int index)。例如,将包含TextView 作为最后一行的TableRow 添加到TableLayout

    TableLayout tl = (TableLayout) findViewById(R.id.my_table_layout);
    TableRow tr = new TableRow(context);
    tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    TextView tv = new TextView(context);
    tv.setText("Some text");
    tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    tr.addView(tv);
    tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
    

    【讨论】:

    • 好的,这是有道理的。谢谢你。我将如何更新表格?
    • 创建TableRows 并使用TableLayout#addView 方法将它们添加到TableLayout。查看更新的答案。
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2017-04-04
    相关资源
    最近更新 更多