TableLayout击碎按钮 [英] TableLayout crushes button
问题描述
我遇到一个奇怪的问题,在Button
旁边有一个TableLayout
.这是XML:
I'm experiencing a strange issue with a TableLayout
next to a Button
. This is the 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
会占用更大的空间,然后将按钮一直推到右侧.
This is confusing me. As you can see, both the Button
and the TableLayout
have a layout_weight
of 1. So I'd expect them to have the same width inside the LinearLayout
but for some reason, the TableLayout
takes up much more space and pushes the button all the way to the right.
请问如何根据layout_weight
中设置的值在TableLayout
和Button
之间分配LinearLayout
中的可用空间?
How can I have the additional space available in the LinearLayout
distributed among the TableLayout
and the Button
according to the value set in layout_weight
please?
请注意,我不想使用RelativeLayout
或ConstraintLayout
.我专门在寻找LinearLayout
解决方案.
Note that I don't want to use a RelativeLayout
or ConstraintLayout
. I'm specifically looking for a LinearLayout
solution.
推荐答案
只需将其添加到您的父母即可.并使两个孩子的宽度<c14>
Just add this to your parent. and make width 0dp
of both child
android:weightSum ="2"
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>
这篇关于TableLayout击碎按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!