TableLayout 压碎按钮
TableLayout crushes button
我遇到了 Button
旁边的 TableLayout
的奇怪问题。这是 XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:stretchColumns="*"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:text="Column 1" />
<Button
android:id="@+id/button1"
android:text="Column 2" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2" />
</LinearLayout>
这就是我得到的:
这让我很困惑。如您所见,Button
和 TableLayout
的 layout_weight
均为 1。所以我希望它们在 LinearLayout
内具有相同的宽度,但对于某些因此,TableLayout
占据了更多 space 并将按钮一直推到右边。
如何根据 layout_weight
中设置的值在 LinearLayout
中分配 TableLayout
和 Button
中可用的额外 space请问?
请注意,我不想使用 RelativeLayout
或 ConstraintLayout
。我正在寻找 LinearLayout
解决方案。
只需将此添加到您的 parent。并使 child
的宽度 0dp
android:weightSum="2"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:stretchColumns="*">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:text="Column 1" />
<Button
android:id="@+id/button1"
android:text="Column 2" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2" />
</LinearLayout>
为 Button
和 TableLayout
设置 android:layout_width="0dp"
我遇到了 Button
旁边的 TableLayout
的奇怪问题。这是 XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:stretchColumns="*"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:text="Column 1" />
<Button
android:id="@+id/button1"
android:text="Column 2" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2" />
</LinearLayout>
这就是我得到的:
这让我很困惑。如您所见,Button
和 TableLayout
的 layout_weight
均为 1。所以我希望它们在 LinearLayout
内具有相同的宽度,但对于某些因此,TableLayout
占据了更多 space 并将按钮一直推到右边。
如何根据 layout_weight
中设置的值在 LinearLayout
中分配 TableLayout
和 Button
中可用的额外 space请问?
请注意,我不想使用 RelativeLayout
或 ConstraintLayout
。我正在寻找 LinearLayout
解决方案。
只需将此添加到您的 parent。并使 child
的宽度0dp
android:weightSum="2"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:stretchColumns="*">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:text="Column 1" />
<Button
android:id="@+id/button1"
android:text="Column 2" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2" />
</LinearLayout>
为 Button
和 TableLayout
android:layout_width="0dp"