我正在尝试将多个视图水平居中RelativeLayout
那是一个基地。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="@android:color/transparent" >
这不起作用。我已经设定centerInParent
to true
对于其中一种观点,这确实有效。但是,我无法使用此解决方案,因为我有两个并排的视图需要居中在一起。尝试优化这一点,所以我想避免嵌套布局,尤其是线性布局。
我是否缺少一些明显的东西?我认为这个属性是针对这种情况而设计的。
我回答了一个涉及三个视图的类似问题,但没有使用嵌套 ViewGroups。
https://stackoverflow.com/a/13279846/1011746 https://stackoverflow.com/a/13279846/1011746
这是在 API 11 中测试的。
对于两个视图水平情况:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="@android:color/black"
>
<Button
android:id="@+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="APPLY"
android:textSize="20sp"
/>
<Button
android:id="@+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="UNDO"
android:textSize="20sp"
android:layout_toRightOf="@id/apply"
/>
</RelativeLayout>
对于两个视图垂直情况:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="@android:color/black"
>
<Button
android:id="@+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="APPLY"
android:textSize="20sp"
/>
<Button
android:id="@+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="UNDO"
android:textSize="20sp"
android:layout_below="@id/apply"
/>
</RelativeLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)