【发布时间】:2016-05-06 00:10:06
【问题描述】:
我有一个 TableLayout,它只包含一行和一个单元格。 我希望我的单元格的内容水平居中。 我尝试了两种方法:
第一种方法,我在 TableRow 元素上设置属性 android:gravity="center_horizontal" 并且它按预期工作:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:gravity="center_horizontal">
<Button
android:id="@+id/MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Hello" />
</TableRow>
</TableLayout>
第二种方法,我直接在单元格内容(此处的按钮)上设置属性 android:layout_gravity="center_horizontal" 但这不会使按钮居中:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<Button
android:id="@+id/MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/Hello" />
</TableRow>
</TableLayout>
有人能解释一下为什么第二种方法不起作用吗?
谢谢
丽安娜
【问题讨论】:
标签: android layout tablelayout gravity