将左边缘对齐到中心 - RelativeLayout

And*_*son 11 android android-layout

我有以下要求(大大简化):

在此输入图像描述

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

我只能用LinearLayout来实现这个目的:

<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>
Run Code Online (Sandbox Code Playgroud)

因为我已经有太多的嵌套视图(因此获取myfile.xml有10个级别,对性能警告不好)我想知道我是否可以使用一个RelativeLayout获得相同的结果.我一直在阅读文档,但我找不到允许我这样做的属性.

Ent*_*eco 36

如果你接受0dp 0dp的空视图作为开销,这很容易.在我看来,这比拥有太多嵌套视图更好,但是这可以作为其他视图的参考.

使用空Space(0x0 px).如果您将其Space水平居中,则可以将其用作参考,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 一个警告:"Space"需要API级别14.如果需要与早期API的兼容性,1x1"LinearLayout"的原始解决方案是有效的. (2认同)