📜  Kotlin 中的 Android 表格布局(1)

📅  最后修改于: 2023-12-03 15:32:30.361000             🧑  作者: Mango

Kotlin 中的 Android 表格布局

在 Android 开发中,表格布局是常用的 UI 布局方式之一。在 Kotlin 中,我们可以使用 TableLayout 和 TableRow 组件来实现表格布局。本文将介绍如何在 Kotlin 中使用 TableLayout 和 TableRow 来实现表格布局,并提供代码示例。

TableLayout 和 TableRow

TableLayout 继承自 ViewGroup,用于显示一个表格布局。它包含多个 TableRow 组件,每个 TableRow 组件表示一行数据。

TableRow 组件继承自 ViewGroup,用于显示一行数据。它包含多个子视图组件,每个子视图组件表示一列数据。

表格布局示例

下面是一个简单的表格布局示例,该布局包含两行数据和三列数据,每个单元格里的数据为一个 TextView 组件:

<TableLayout
    android:id="@+id/tableLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:stretchColumns="*">

    <TableRow>
        <TextView
            android:text="Name"
            android:textStyle="bold" />
        <TextView
            android:text="Age"
            android:textStyle="bold" />
        <TextView
            android:text="Gender"
            android:textStyle="bold" />
    </TableRow>

    <TableRow>
        <TextView
            android:text="John"
            android:id="@+id/nameTextView" />
        <TextView
            android:text="30"
            android:id="@+id/ageTextView" />
        <TextView
            android:text="Male"
            android:id="@+id/genderTextView" />
    </TableRow>

    <TableRow>
        <TextView
            android:text="Lucy"
            android:id="@+id/nameTextView" />
        <TextView
            android:text="25"
            android:id="@+id/ageTextView" />
        <TextView
            android:text="Female"
            android:id="@+id/genderTextView" />
    </TableRow>

</TableLayout>

android:stretchColumns="*" 属性将使每列产生相等的空间。

Kotlin 代码实现

我们可以使用 findViewById() 方法获取 TableLayout 和 TableRow 组件,然后使用 addView() 方法向 TableRow 组件添加子视图组件:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val tableLayout = findViewById<TableLayout>(R.id.tableLayout)

        val row1 = TableRow(this)
        val name1 = TextView(this)
        name1.text = "John"
        row1.addView(name1)
        val age1 = TextView(this)
        age1.text = "30"
        row1.addView(age1)
        val gender1 = TextView(this)
        gender1.text = "Male"
        row1.addView(gender1)
        tableLayout.addView(row1)

        val row2 = TableRow(this)
        val name2 = TextView(this)
        name2.text = "Lucy"
        row2.addView(name2)
        val age2 = TextView(this)
        age2.text = "25"
        row2.addView(age2)
        val gender2 = TextView(this)
        gender2.text = "Female"
        row2.addView(gender2)
        tableLayout.addView(row2)
    }

}

我们首先使用 findViewById() 方法获取 TableLayout 组件。然后,创建一个新的 TableRow 组件,创建三个 TextView 组件来表示每列数据,将这三个 TextView 组件添加到 TableRow 组件中,最后将 TableRow 组件添加到 TableLayout 组件中。

总结

使用 TableLayout 和 TableRow 组件可以轻松地实现表格布局。在 Kotlin 中,我们可以使用 findViewById() 和 addView() 方法来获取和操作这两个组件,从而实现灵活的表格布局。