android - 将左边缘对齐到中心 - RelativeLayout

标签 android android-layout

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

enter image description here

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

因为我已经有太多的嵌套 View (因此得到一个 myfile.xml 有更多的 10 级,不利于性能 警告)我想知道我是否可以得到相同的结果一个 RelativeLayout。我经历过 documentation但我找不到允许我这样做的属性(property)。

最佳答案

如果您接受 0dp 的 1 个空 View 作为开销,这将非常容易。在我看来,这比嵌套 View 太多要好,但这可以作为其他 View 的引用。

使用空的 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>

关于android - 将左边缘对齐到中心 - RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16445560/

相关文章:

php - 在没有 GCM 的情况下将数据发送到特定设备

android - 具有从 URI 解析的 json 的多级 ExpandableListView

java - 自定义对话框的宽度和高度

android - 如何将我的 dialogFragment 显示为圆角?

java - Android 应用内结算 : Ads are not removing when In-App is made

java - Android java 2.3.3,如何创建类似steam的菜单?

android - 如何在我的布局中将 2 个按钮放置在同一位置

android - 如何调整 ScrollView 的大小以适合底部 60% 的屏幕?

android - 在多个 TextView 中使用时选取框不起作用

java - 我在菜单项上膨胀了自定义布局以显示计数