[英]Android RadioButton in TableLayout with gravity not showing
[英]Android TableLayout and horizontal gravity
我有一个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:gravity
引用视图中的子内容。 在您的情况下,这会影响表格单元格的内容。
android:layout_gravity
表示视图在其父级中的位置。 在这种情况下,表格中单元格的位置,而不是单元格内容。 单元格内容不会受到影响。
有关其他示例和更多说明,请参见此类似问题: Android上的Gravity和layout_gravity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.