1

I came across a negative value assigned for an android button proerty as follows.

android:layout_marginTop="-37px"

Does anyone have any idea of what exactly this means...??? Thanks in advance...

JibW
  • 4,538
  • 17
  • 66
  • 101

1 Answers1

4

A negative margin can be used to make a View appear smaller to the layout manager when it is been positioned.

So, for example imagine a View with a height of h and a marginTop of -m. When this view is positioned, the manager will consider the top of the view to be -m rather the 0. In a linear layout situation (assuming vertical layout), this would cause the view to be rendered on top of the previous view.

You can see this in the example below, as you decrease the top margin of the textView2, it becomes overlaid on textView1.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-15dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Whether this is specified behaviour or not, I'm not 100% sure. In this post Romain Guy mentions that you can use negative margins, however in this post on Google Groups he mentioned that negative margins behaviour is unspecified.

Community
  • 1
  • 1
Kingamajick
  • 2,281
  • 1
  • 16
  • 19