我有以下要求(大大简化):
文本 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/