LinearLayout, RelativeLayout, etc. margins do not work as expected
Asked Answered
R

4

25

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.

Replevin answered 23/3, 2011 at 23:14 Comment(1)
try setting the button center horizontalRespectful
P
28

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.

Perfoliate answered 23/3, 2011 at 23:30 Comment(1)
Margin should margin the LinearLayout from all sides by 40dp. Its strange that it's not working as expected.Indigestion
H
13

The problem is actually the way FrameLayout interprets margins. setContentView() attaches your "main" layout to a FrameLayout, which is the actual root of the view hierarchy (you can see that with Hierarchy Viewer) and is offered to you by the phone.

Margins are managed by the parent layout, so in this case that main FrameLayout. I don't know if it's a feature or a bug, but that's how this layout interprets margins.

So well, the solution was already posted while I was typing: use padding instead.

Hoo answered 23/3, 2011 at 23:33 Comment(2)
I recall fixing some bugs with the way that FrameLayout handles margins during the Honeycomb timeframe and it involved the default gravity setting. It may indeed be a bug. :)Merozoite
The another way is set android:layout_gravity="top|left" for View that inside FrameLayout.Erector
Y
7

if you need set margin for a layout, simply wrap it with another linear or relative layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <LinearLayout 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>

</LinearLayout>
Yuhas answered 23/8, 2011 at 22:47 Comment(2)
This is the better answer. Padding isn't necessarily what you want, particularly if your layout has a visible background.Favorite
You should avoid nesting layouts just for the purpose of giving margin to the inner one. developer.android.com/training/improving-layouts/…. This is not the better answer in any way.Geddes
I
0

Wrapping the Linear Layout with another layout is the best strategy.

Incendiarism answered 17/3, 2019 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.