表格行中带有按钮和文本视图的布局权重
我试图制作四行,每行三个按钮,按钮底部有两个文本视图。我希望它们全部均匀分布,但事实并非如此,我不确定为什么。
这是我的 main.xml
文件中的代码。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
android:id="@+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button01"
android:text="@string/str_1"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button02"
android:text="@string/str_2"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button03"
android:text="@string/str_3"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button04"
android:text="@string/str_4"/>
</TableRow>
<TableRow android:id="@+id/TableRow02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button05"
android:text="@+string/str_5"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button06"
android:text="@+string/str_6"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button07"
android:text="@+string/str_7"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button08"
android:text="@+string/str_8"/>
</TableRow>
<TableRow android:id="@+id/TableRow03"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button09"
android:text="@+string/str_9"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button10"
android:text="@+string/str_10"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button11"
android:text="@+string/str_11"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button12"
android:text="@+string/str_12"/>
</TableRow>
<TableRow android:id="@+id/TableRow04"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<TextView android:text="@+id/TextView01"
android:layout_weight="1"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:text="@+id/TextView02"
android:layout_weight="1"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</TableRow>
</TableLayout>
我的文本视图具有以下代码:
TextView t = (TextView)findViewById(R.id.TextView01);
TextView t2 = (TextView)findViewById(R.id.TextView02);
t.setTextSize(24);
t2.setTextSize(24);
当我运行程序时,我得到:
I am trying to make four rows of three buttons each, and at the bottom of the buttons are two text views. I would like for them all to be spaced evenly but they aren't and I am not sure why.
Here is the code in my main.xml
file.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
android:id="@+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button01"
android:text="@string/str_1"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button02"
android:text="@string/str_2"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button03"
android:text="@string/str_3"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button04"
android:text="@string/str_4"/>
</TableRow>
<TableRow android:id="@+id/TableRow02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button05"
android:text="@+string/str_5"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button06"
android:text="@+string/str_6"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button07"
android:text="@+string/str_7"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button08"
android:text="@+string/str_8"/>
</TableRow>
<TableRow android:id="@+id/TableRow03"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button09"
android:text="@+string/str_9"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button10"
android:text="@+string/str_10"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button11"
android:text="@+string/str_11"/>
<Button android:onClick="button_click"
android:layout_weight="1"
android:id="@+id/Button12"
android:text="@+string/str_12"/>
</TableRow>
<TableRow android:id="@+id/TableRow04"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<TextView android:text="@+id/TextView01"
android:layout_weight="1"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:text="@+id/TextView02"
android:layout_weight="1"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</TableRow>
</TableLayout>
My text views have the following code:
TextView t = (TextView)findViewById(R.id.TextView01);
TextView t2 = (TextView)findViewById(R.id.TextView02);
t.setTextSize(24);
t2.setTextSize(24);
When I run my program I get:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,为了帮助解决此问题,请在表格布局中添加
android:stretchColumns="0,1,2,3"
,这将使按钮大小均匀。其次,将
android:layout_span="2"
添加到两个文本视图中。这是最终代码:
如果您以编程方式设置文本大小以使 TextView 均匀分布,则在设置
android:layout_span
时不需要它。Ok so to help out with this add
android:stretchColumns="0,1,2,3"
in your Table Layout which would make buttons even sized.Secondly add a
android:layout_span="2"
to both of your text views.Heres you a final code:
If you are setting the text size programmatically to make your TextViews evenly spaced, its not needed when you have
android:layout_span
set.