我有一个只包含一行和一个单元格的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 =&#34; center_horizontal&#34; ,但这并不是按钮的中心:
<?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>
有人可以解释为什么第二种方法不起作用吗?
谢谢
Riana
答案 0 :(得分:0)
android:gravity
指视图中的子内容。在您的情况下,这会影响表格单元格的CONTENTS。
android:layout_gravity
指的是视图在其父级中的位置。在这种情况下,单元格在表格中的位置,而不是单元格内容。细胞内容不会受到影响。
对于其他一些示例和进一步说明,请参阅此类似问题:Gravity and layout_gravity on Android