Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout, RelativeLayout, etc. margins do not work as expected

Tags:

Margins in group layouts do not seem to work.

For example,

<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_margin="40dip"     android:layout_width="match_parent"     android:layout_height="match_parent">      <Button         android:id="@+id/button"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:text="I'm a button" />  </LinearLayout> 

should display a button with 40p margins on all sides. However, it has 80p margins on the right and bottom.

Am I doing something wrong? Is this a bug?

A workaround would be to use gravity, but this only works with even margins.

BTW, there is a similar question posted here but has not been answered.

like image 552
Matt Avatar asked Mar 23 '11 23:03

Matt


People also ask

What is difference between LinearLayout and RelativeLayout?

Android Layout Types Android provides the following ViewGroups or layouts: LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions.

What is the difference between LinearLayout and Framelayout?

Frame Layout: This is designed to block out an area on the screen to display a single item. Linear Layout: A layout that arranges its children in a single column or a single row. Relative Layout: This layout is a view group that displays child views in relative positions.

Is ConstraintLayout faster than LinearLayout?

Results show that the fastest layout is Relative Layout, but difference between this and Linear Layout is really small, what we can't say about Constraint Layout. More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.


1 Answers

android:padding="40dp" on the LinearLayout or android:layout_margin="40dp" on the Button will give you the effect you want. Padding defines the space between a views edges and its content, layout margin defines extra space on the sides of a view.

like image 62
Robby Pond Avatar answered Sep 28 '22 14:09

Robby Pond