프로그래밍/안드로이드

[안드로이드] Margin 과 Padding의 차이점과 사용방법 - (마진과 패딩 차이점)

오치리일상 2017. 8. 25.

*Magin과 Padding의 차이점과 사용방법

 

 

안드로이드 개발시 위젯을 위치할 때, margin과 padding을 사용하여 임의의 수치만큼 여백을 둘 수 있다.

 

 

 

margin 과 padding의 사용되는 상황을 아래 그림으로 살펴본다.

 

 

 

 

위 그림을 보면 주황색의 TextView 위젯이 놓여져있다. 그 뒤에는 초록색의 부모 레이아웃이 위치한다.

 

Margin : 위젯(여기서는 TextView)이 부모 레이아웃의 테두리로부터의 여을 말한다. android:layout_marginTop, android:layout_marginBottom, android:layout_margin_left, android:layout_marginRight 의 속성으로 상하좌우의 여백을 각각 지정할 수 있다.

 

Padding : 위젯(여기서는 TextView) 테두리로부터 위젯 안에의 내용(여기서는 text)사이의 여백을 말한다. android:paddingTop, android:paddingBottom, android:paddingLeft, android:paddingRight 의 속성으로 상하좌우의 여백을 각각 지정할 수 있다. 

 

 

아래 에제 소스로 확인 해본다.

 

* activity_main.xml

<?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">

    <!--margin, padding 적용전-->
    <!--상하좌우에 여백이 전혀없다-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="margin, padding 적용전"
        android:textColor="#000000"
        android:background="#ff77ff" />

    <!--margin만 적용한 예-->
    <!--부모레이아웃과 TextView 위젯에 20dp의 여백을 가진다.-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="margin만 적용한 예"
        android:textColor="#000000"
        android:background="#ff7777"/>

    <!--padding만 적용한 예-->
    <!--TextView의 테두리로부터 text의 사이에 20dp의 여백을 가진다.-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="padding만 적용한 예"
        android:textColor="#000000"
        android:background="#7777ff"/>

    <!--margin과 padding 모두 적용한 예-->
    <!--부모레이아웃과 TextView 위젯에 20dp의 여백을 가진다.-->
    <!--TextView의 테두리로부터 text의 사이에 20dp의 여백을 가진다.-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:padding="20dp"
        android:text="margin과 padding 모두 적용한 예"
        android:textColor="#000000"
        android:background="#77ffff" />


</LinearLayout>

 

아래는 위 소스의 실행화면이다.

 

 

 

 

댓글