Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android layout margins with percentage

I want to set margins by percentage.. I have 4 imageviews in a linearlayout and want to set default left,right,top,bottom margins that keep same percentage for each screen size.

is it possible ?

here is a demo what i want.. enter image description here

And here is what i've tried and doesn't work

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >      <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_weight="1"         android:weightSum="10" >          <Thumbnail             android:id="@+id/thumb1"             android:layout_width="0dp"             android:layout_height="match_parent"             android:layout_weight="4" />          <Thumbnail             android:id="@+id/thumb2"             android:layout_width="0dp"             android:layout_height="match_parent"             android:layout_weight="4" />      </LinearLayout>      <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_weight="1"         android:weightSum="10" >           <Thumbnail              android:id="@+id/thumb3"              android:layout_width="0dp"              android:layout_height="match_parent"              android:layout_weight="4" >          </Thumbnail>          <Thumbnail             android:id="@+id/thumb4"             android:layout_width="0dp"             android:layout_height="match_parent"             android:layout_weight="4" />      </LinearLayout>  </LinearLayout> 

Thanks for your help

like image 441
dracula Avatar asked Apr 24 '13 19:04

dracula


People also ask

How do you use percent constraint layout?

You can currently do this in a couple of ways. One is to create guidelines (right-click the design area, then click add vertical/horizontal guideline). You can then click the guideline's "header" to change the positioning to be percentage based. Finally, you can constrain views to guidelines.

Can we give negative margin in Android?

No, you should not use negative margin .

What is the difference between margin and padding in Android layout?

Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent). Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object.


1 Answers

You can have invisible Views in your LinearLayouts as spacers and use the layout_weight mechanism to assign them relative size.

Example:

<LinearLayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_weight="1">      <Thumbnail         android:id="@+id/thumb1"         android:layout_width="0dp"         android:layout_height="match_parent"         android:layout_weight="4" />      <View         android:layout_width="0dp"         android:layout_height="0dp"         android:layout_weight="2"         android:visibility="invisible"/>      <Thumbnail         android:id="@+id/thumb2"         android:layout_width="0dp"         android:layout_height="match_parent"         android:layout_weight="4" />  </LinearLayout> 
like image 128
laalto Avatar answered Sep 22 '22 02:09

laalto