将左边缘与中心对齐 - RelativeLayout

11

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

图片说明

文本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>

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


使用两个LinearLayout来创建该行,并使用布局权重作为总权重的50%。 - Its not blank
如果我添加第三个布局,它不会放置在第一个布局下面。 - IntoTheDeep
5个回答

37

如果你愿意接受一个0dp x 0dp的空视图,作为开销的话,这将非常容易。在我看来,这比有太多嵌套视图要好,但这可以作为其他视图的参考。

使用一个空的 Space(大小为0x0像素)。如果水平居中这个 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>

有趣!如果没有更好的,我接受这个,尽管“dummy”仍然会给我那个警告。 - Androiderson
好的,那么你已经深入了10个级别。在这种情况下,我肯定更喜欢这个选项(它添加了1个“无用”的线性布局),而不是weights=1选项(它添加了2个“无用”的线性布局并增加了额外的嵌套级别)!祝你好运。 - Entreco
确实!顺便说一句,使用 TabHost 很难不达到10级。 - Androiderson
1
我建议使用Space而不是空的LinearLayout。使用widthheight0dpSpace进行测试,效果如预期。 :) - 7hi4g0
我之前不知道 Space 视图,真棒!它为我节省了嵌套两个布局并使用中心对齐的麻烦。 - Daniel Handojo
2
一个警告:使用Space需要API级别14。如果需要与早期API兼容,则可以使用一个1x1的LinearLayout的原始解决方案。 - mike47

0

尝试使用以下代码(未经测试):

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dip" >

        <!--  ListRow Left side text-->

        <TextView
            android:id = "@+id/lefttextview" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="text1"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
        <!-- center -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/lefttextview"
            android:text="text2"/>


    </RelativeLayout>

0

你可以使用 alignParentLeft="true" 将它对齐到父元素的左侧。


0

无法理解具体的问题。您想要的是从中心获取text2,因此首先使用相对布局,因为在相对布局中更容易进行控件的定位,而不是线性布局,后者仅定义方向并将控件一个接一个地放置。 在相对布局中,您可以将属性center_in_parent设置为true,然后将text1与其左侧对齐。我希望这能解决您的问题。


0
你可能需要使用一个带有android:layout_centerHorizontal="true"android:layout_width="0dp"属性的虚拟View,并且指定一个id。然后使用属性android:layout_toRightOf="@id/id_of_dummy_view"添加你的TextView来定位它。可能有更简单的方法来实现这个,但我目前无法测试。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接