在 Android 中,TableRow 是一个特殊的 LinearLayout,它通常被用作 TableLayout 的子元素来表示表格中的一行。TableRow 继承了 LinearLayout 的特性,因此你可以在其中添加任何 View 对象,如 TextView、EditText、Button、CheckBox、RadioButton、ImageView 等。
然而,TableRow 有一个特定的用途,那就是在 TableLayout 中定义表格行。在 TableLayout 中,TableRow 并不需要显式地指定布局参数(如 layout_width 和 layout_height),因为这些参数会由 TableLayout 自动管理。
以下是一个使用 TableLayout 和 TableRow 的基本示例:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView android:text="列1" />
<TextView android:text="列2" />
<TextView android:text="列3" />
</TableRow>
<TableRow>
<EditText android:hint="输入内容" />
<CheckBox android:text="选项" />
<Button android:text="按钮" />
</TableRow>
<!-- 更多 TableRow ... -->
</TableLayout>
在这个例子中,我们创建了一个包含两行的表格。第一行包含三个 TextView 控件,它们分别代表表格的列标题。第二行包含一个 EditText、一个 CheckBox 和一个 Button。你可以根据需要添加更多的 TableRow 和 View 控件来构建更复杂的表格。
需要注意的是,尽管 TableRow 继承了 LinearLayout,但通常不建议在 TableRow 中嵌套其他 LinearLayout 或 TableLayout,因为这可能会导致布局上的复杂性和不可预测的行为。如果你需要更复杂的布局,可能需要考虑使用其他布局容器,如 RelativeLayout 或 ConstraintLayout。
1769

被折叠的 条评论
为什么被折叠?



