본문 바로가기

Programming/Android

3. 사용자 인터페이스 구현 (3) - 뷰의 기초 공통 속성 3

android studio logo image

 

 

○ margin, padding 

 

잘 알고 있겠지만, margin은 뷰와 뷰 사이의 간격을 지정하는 속성입니다. 그리고 padding은 뷰 내부에서 뷰의 콘텐츠(내용)와 뷰의 테두리 사이의 간격을 지정하는 속성입니다. 뷰는 기본적으로 margin과 padding이 디폴트로 정해져 있는 값이 있는데, 해당 속성을 직접 지정함으로써 이를 변경할 수 있는 것이죠. 

 

기본적으로 margin과 padding 속성 값을 지정하게 되면 네 방향 간격이 모두 같은 크기로 설정됩니다. 상/하/좌/우를 각각 단일 방향으로 조정하고 싶다면, 아래와 같이 속성을 지정해야 합니다. 

 

· 단일 방향 margin 속성
 - layout_marginLeft, layout_marginRight, layout_marginTop, layout_marginBottom

· 단일 방향 padding 속성
 - paddingLeft, paddingRight, paddingTop, paddingBottom

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".Sample_2Activity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BUTTON1"
        android:padding="24dp" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BUTTON2"
            android:layout_marginLeft="16dp" />
    
    </LinearLayout>