Android 使单元格具有相同的宽度和高度以填充表格
我正在尝试学习如何在android中使用表格布局,以下是我的代码:Android 使单元格具有相同的宽度和高度以填充表格,android,android-tablelayout,Android,Android Tablelayout,我正在尝试学习如何在android中使用表格布局,以下是我的代码: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_hei
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 3 columns -->
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip" >
<TextView
android:id="@+id/textView2"
android:text="Column 1" />
<Button
android:id="@+id/button2"
android:text="Column 2" />
<Button
android:id="@+id/button3"
android:text="Column 3" />
</TableRow>
</TableLayout>
这就是结果:
如何使
TableRow
与TableLayout
的宽度和高度匹配?简单地说,我想让所有宽度相同的列和高度相同的行填充表格布局
,可以吗?如何操作?您需要使用权重值。两行的xml如下所示-
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 3 columns -->
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dip" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Column 1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Column 2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Column 3" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dip" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Column 1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Column 2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Column 3" />
</TableRow>
</TableLayout>
这就是结果-
请注意,不推荐使用fill\u parent
,而应使用match\u parent
。最快的方法是对表行中的对象使用layout\u weight
。下面是xml在指定后的外观
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 3 columns -->
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip" >
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Column 1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Column 2" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Column 3" />
</TableRow>
</TableLayout>
使用权重时,需要将布局宽度设置为0dp,以便覆盖实际值。此拆分将使每个视图占据TableRow的1/3。玩转权重值,直到你得到你喜欢的东西。将android:layout\u weight=“0.3”
添加到孩子的内部TableRow
尝试将布局xml替换为此格式
希望这就是你想要的
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 3 columns -->
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="5dip" >
<TextView
android:id="@+id/textView2"
android:layout_weight="0.3"
android:text="Column 1" />
<Button
android:id="@+id/button2"
android:layout_weight="0.3"
android:text="Column 2" />
<Button
android:id="@+id/button3"
android:layout_weight="0.3"
android:text="Column 3" />
</TableRow>
</TableLayout>