android – 对齐左边缘到中心 – RelativeLayout

前端之家收集整理的这篇文章主要介绍了android – 对齐左边缘到中心 – RelativeLayout前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下要求(大大简化了):

文本2必须从屏幕的CENTER开始.

我只能使用LinearLayout来实现:

@H_404_6@<more_code></more_code><LinearLayout android:baselineAligned="false" android:weightSum="2" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_weight="1" android:orientation="horizontal" android:layout_width="0dp" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test one"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test two"/> </LinearLayout> <LinearLayout android:layout_weight="1" android:orientation="horizontal" android:layout_width="0dp" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test three"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test four"/> </LinearLayout> </LinearLayout><more_code></more_code>

因为我已经有太多的嵌套视图(因此得到一个myfile.xml有更多的10个级别,不好的性能警告)我想知道如果我可以得到与一个RelativeLayout相同的结果.我一直在documentation,但我找不到一个允许我的财产.

解决方法

如果您将0dp作为开销接受0dp的1个空视图,这很简单.在我看来,这比嵌套的观点更好,但这可以作为其他观点的参考.

使用空的空格(0x0像素).如果你把这个空间水平定位,你可以用它作为参考:

@H_404_6@<RelativeLayout> <!-- Empty layout (0x0 dp) centered horizontally --> <Space android:id="@+id/dummy" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerHorizontal="true" android:visibility="invisible"/> <!-- Align to parent left --> <TextView android:id="@+id/test_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="test one"/> <!-- to right of the prevIoUs one,and to left of the dummy --> <TextView android:id="@+id/test_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/test_one" android:layout_toLeftOf="@+id/dummy" android:text="test two"/> <!-- Align to right of the dummy --> <TextView android:id="@+id/test_three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/dummy" android:text="test three"/> <!-- Align to right of the prevIoUs one,and parent right --> <TextView android:id="@+id/test_four" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/test_three" android:layout_alignParentRight="true" android:text="test four"/> </RelativeLayout>

猜你在找的Android相关文章