我有以下要求(大大简化):
text 2必须从屏幕的中心开始。
我只能使用线性布局的:
<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 个级别,对性能不利警告)我想知道我是否能得到相同的结果相对布局。我已经经历过文档 http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html但我找不到允许我这样做的财产。
如果您接受 1 个 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)