Last active
August 29, 2015 14:22
-
-
Save saggafarsyad/fb2fce6b3f21b701519c to your computer and use it in GitHub Desktop.
Android - Optimize LinearLayout and layout_weight using RelativeLayout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--I mostly use LinearLayout and layout_weight to give views the same width or height in a layout--> | |
<!--For example--> | |
<LinearLayout | |
android:orientation="horizontal" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<Button | |
android:layout_weight="1" | |
android:layout_height="wrap_content" | |
android:layout_width="0dp"/> | |
<Button | |
android:layout_weight="1" | |
android:layout_height="wrap_content" | |
android:layout_width="0dp"/> | |
</LinearLayout> | |
<!-- In certain case, such as a 2x2 button, --> | |
<!-- you'll need a Vertical LinearLayout with 2 Horizontal LinearLayout and 2 Buttons in each layout--> | |
<!-- Like this--> | |
<LinearLayout> | |
<LinearLayout> | |
<Button /> | |
<Button /> | |
</LinearLayout> | |
<LinearLayout> | |
<Button /> | |
<Button /> | |
</LinearLayout> | |
</LinearLayout> | |
<!--this solution will end up with more deeper UI hierarchy and it is not recommended because it can let performance down--> | |
<!--Now, let's check OptimizedLayout.xml--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- So, I found this solution in StackOverflow --> | |
<!-- http://stackoverflow.com/a/7784186/2950382 --> | |
<!-- He or She suggest a RelativeLayout and add a dummy view, place it in centerHorizontalParent--> | |
<!-- For example... --> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<!-- Here is the dummies!--> | |
<View | |
android:layout_centerHorizontal="true" | |
android:id="@+id/splitter" | |
android:layout_width="0dp" | |
android:layout_height="0dp" /> | |
<Button | |
android:layout_alignParentTop="true" | |
android:layout_alignParentLeft="true" | |
android:layout_toLeftOf="@id/splitter" | |
android:id="@+id/button_1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<Button | |
android:layout_alignParentTop="true" | |
android:layout_alignParentRight="true" | |
android:layout_toRightOf="@+id/splitter" | |
android:id="@+id/button_2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<Button | |
android:layout_toLeftOf="@id/splitter" | |
android:layout_alignParentLeft="true" | |
android:layout_below="@+id/button_1" | |
android:id="@+id/button_3" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<Button | |
android:layout_toRightOf="@id/splitter" | |
android:layout_below="@id/button_2" | |
android:layout_alignParentRight="true" | |
android:id="@+id/button_4" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment