Difference between a View's Padding and Margin

后端 未结 14 2911
渐次进展
渐次进展 2020-11-22 06:45

What is the difference between a View\'s Margin and Padding?

相关标签:
14条回答
  • 2020-11-22 07:01

    Below image will let you understand the padding and margin-

    enter image description here

    0 讨论(0)
  • 2020-11-22 07:03

    Let's just suppose you have a button in a view and the size of the view is 200 by 200, and the size of the button is 50 by 50, and the button title is HT. Now the difference between margin and padding is, you can set the margin of the button in the view, for example 20 from the left, 20 from the top, and padding will adjust the text position in the button or text view etc. for example, padding value is 20 from the left, so it will adjust the position of the text.

    0 讨论(0)
  • 2020-11-22 07:07

    In simple words:

    1. Padding - creates space inside the view's border.
    2. Margin - creates space outside the view's border.
    0 讨论(0)
  • 2020-11-22 07:08

    Padding
    Padding is inside of a View.For example if you give android:paddingLeft=20dp, then the items inside the view will arrange with 20dp width from left.You can also use paddingRight, paddingBottom, paddingTop which are to give padding from right, bottom and top respectively.

    Margin
    Margin is outside of a View. For example if you give android:marginLeft=20dp , then the view will be arranged after 20dp from left.

    0 讨论(0)
  • 2020-11-22 07:10

    Padding means space between widget and widget original frame. But the margin is space between widget's original frame to boundaries other widget's frame..

    0 讨论(0)
  • 2020-11-22 07:11

    To help me remember the meaning of padding, I think of a big coat with lots of thick cotton padding. I'm inside my coat, but me and my padded coat are together. We're a unit.

    But to remember margin, I think of, "Hey, give me some margin!" It's the empty space between me and you. Don't come inside my comfort zone -- my margin.

    To make it more clear, here is a picture of padding and margin in a TextView:

    xml layout for the image above

    <?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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="#c5e1b0"
            android:textColor="#000000"
            android:text="TextView margin only"
            android:textSize="20sp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="#f6c0c0"
            android:textColor="#000000"
            android:text="TextView margin only"
            android:textSize="20sp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#c5e1b0"
            android:padding="10dp"
            android:textColor="#000000"
            android:text="TextView padding only"
            android:textSize="20sp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#f6c0c0"
            android:padding="10dp"
            android:textColor="#000000"
            android:text="TextView padding only"
            android:textSize="20sp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="#c5e1b0"
            android:textColor="#000000"
            android:padding="10dp"
            android:text="TextView padding and margin"
            android:textSize="20sp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="#f6c0c0"
            android:textColor="#000000"
            android:padding="10dp"
            android:text="TextView padding and margin"
            android:textSize="20sp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#c5e1b0"
            android:textColor="#000000"
            android:text="TextView no padding no margin"
            android:textSize="20sp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#f6c0c0"
            android:textColor="#000000"
            android:text="TextView no padding no margin"
            android:textSize="20sp" />
    
    </LinearLayout>
    

    Related

    • Gravity vs layout_gravity
    • Match_parent vs wrap_content
    0 讨论(0)
提交回复
热议问题