How to divide space between elements? Android



Lastly I've met with some interesting problem. When I want to precisely divide place on screen between some elements I usually use LinearLayout, put inside my elements and give them some weights. It works great for 2 elements and little more, but when I want to divide screen on more different parts (20 | 60 | 20) and then divide them also, it becomes very frustrating.


How should I do it? I want my layout look the same on different screens, so margin will look different on small and large screen. So for example if I want my button to take 60% of screens width I am forced to add 20% spaces on both sides.


I actually made it using weights, but my xml code looks like this:



<LinearLayout
. . .
android:orientation="vertical"
android:weightSum="0">


<TextView
. . .
android:layout_weight="67"/>

<LinearLayout
. . .
android:orientation="horizontal"
android:layout_weight="59"
android:weightSum="0">


<TextView
. . .
android:layout_weight="80"/>

<ImageView
. . .
android:layout_weight="10"/>

<TextView
. . .
android:layout_weight="80"/>



</LinearLayout>

<TextView
. . .
android:layout_weight="50"/>

<LinearLayout
. . .
android:orientation="horizontal"
android:weightSum="0">


<TextView
. . .
android:layout_weight="70"/>

<LinearLayout
. . .
android:orientation="vertical"
android:weightSum="0">

<TextView
. . .
android:layout_weight="65"/>

<EditText
. . .
android:layout_weight="50"/>

<TextView
. . .
android:layout_weight="75"/>

<EditText
. . .
android:layout_weight="50"/>

<TextView
. . .
android:layout_weight="65"/>


</LinearLayout>

<TextView
. . .
android:layout_weight="70"/>


</LinearLayout>

<LinearLayout
. . .
android:orientation="horizontal"
android:weightSum="0">

<TextView
. . .
android:layout_weight="80"/>

<LinearLayout
. . .
android:orientation="vertical"
android:layout_weight="60">

<TextView
. . .
android:layout_weight="80"/>

<Button
. . .
android:layout_weight="60"/>

<TextView
. . .
android:layout_weight="50"/>

</LinearLayout>


<TextView
. . .
android:layout_weight="80"/>



</LinearLayout>

<LinearLayout
. . .
android:orientation="horizontal"
android:layout_weight="65"
android:weightSum="0"
>

<Button
android:layout_weight="1"
. . ./>

<Button
android:layout_weight="1"
. . ./>

<Button
android:layout_weight="1"
. . ./>

</LinearLayout>


As you can see there are a lot of empty TextViews which do nothing except taking some place on layout. Is there more intelligent and less frustrating way to precisely divide place on screen between elements?


Glad to hear any advises!


No comments:

Post a Comment