android - Align left edge to center - RelativeLayout -
i have following requirement (drastically simplified):
text 2 must start center of screen.
i achieve using linearlayout's:
<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>
since have many nested views (thus getting myfile.xml has more 10 levels, bad performance warning) i'd know if can same result 1 relativelayout. i've been through documentation not find property allows me that.
this easy if accept 1 empty view of 0dp 0dp overhead. in opinion better having many nested views, can serve reference other views.
use empty space
(of 0x0 px). if center space
horizontally, can use reference so:
<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 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"/> <!-- right of previous one, , left of 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 right of 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 right of previous one, , 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>
Comments
Post a Comment