๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Android Studio

[Android Studio] Margin, Padding, Gravity, ์Šคํƒ€์ผ ๋ฐ ํ…Œ๋งˆ (# ์ถ”๊ฐ€ํ•˜๊ธฐ)

by ์ ผ์ ผ39 2024. 3. 19.

1. Margin

    : view์™€ ๋‹ค๋ฅธ ๋ทฐ(์ปจํ…Œ์ด๋„ˆ) ๊ฐ„์˜ ๊ฐ„๊ฒฉ

    - ์ƒํ•˜์ขŒ์šฐ๋กœ ๋™์ผํ•œ ๋งˆ์ง„ ์„ค์ •์‹œ์— ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ : layout_margin

    - 4๋ฐฉํ–ฅ์˜ ๋งˆ์ง„์„ ๊ฐ๊ฐ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•  ๋•Œ
        : layout_marginLeft, layout_marginRight, layout_marginTop, layout_marginBottom

2. Padding

    : ์ƒํ•˜์ขŒ์šฐ๋กœ ๋™์ผํ•œ ํŒจ๋”ฉ ์„ค์ • ์‹œ์— ์‚ฌ์šฉ (์•ˆ์ชฝ์œผ๋กœ!) : padding

    - 4๋ฐฉํ–ฅ์˜ ๋งˆ์ง„์„ ๊ฐ๊ฐ ๋‹ค๋ฅด๊ฒŒ ์„ค์ • ์‹œ
        : paddingLeft, paddingRight, paddingTop, paddingBottom

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout ...
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="math_parent"
        android:layout_height="marth_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_margin="40dp"
        android:padding="20dp"
        android:background="#aa00ff"
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button"/>
    </LinearLayout>
    ...
</LinearLayout>

 

3. Gravity

    1) Gravity ์†์„ฑ : ํ•ด๋‹น ๋ทฐ ์•ˆ์˜ ๋‚ด์šฉ๋ฌผ(ํ…์ŠคํŠธ) ์œ„์น˜์— ๋Œ€ํ•œ ์ •๋ ฌ ๋ฐฉ์‹์„ ์ง€์ •

    2) layout_gravity ์†์„ฑ : ๋ถ€๋ชจ ๋ทฐ ์•ˆ์—์„œ ํ•ด๋‹น ๋ทฐ์˜ ์ •๋ ฌ ๋ฐฉ์‹ ์ง€์ •

BOTTOM ๋ถ€๋ชจ ๋ทฐ์—์„œ ์•„๋ž˜์ชฝ์— ์œ„์น˜์‹œํ‚ด
CENTER ๋ถ€๋ชจ ๋ทฐ์˜ ์ค‘์•™์— ์œ„์น˜์‹œํ‚ด
CENTER_HORIZONTAL ๋ถ€๋ชจ ๋ทฐ์˜ ์ˆ˜ํ‰๊ธฐ์ค€ ์ค‘์•™์— ์œ„์น˜์‹œํ‚ด
CENTER_VERTICAL ๋ถ€๋ชจ ๋ทฐ์˜ ์ˆ˜์ง๊ธฐ์ค€ ์ค‘์•™๋ฐ ์œ„์น˜์‹œํ‚ด
END ๋ถ€๋ชจ ๋ทฐ์—์„œ ํ…์ŠคํŠธ ๋ฐฉํ–ฅ์˜ ๋(ํ•œ๊ธ€, ์˜์–ด๋Š” ์˜ค๋ฅธ์ชฝ)์— ์œ„์น˜์‹œํ‚ด
LEFT ๋ถ€๋ชจ ๋ทฐ์—์„œ ์™ผ์ชฝ์— ์œ„์น˜์‹œํ‚ด
RIGHT ๋ถ€๋ชจ ๋ทฐ์—์„œ ์˜ค๋ฅธ์ชฝ์— ์œ„์น˜์‹œํ‚ด
TOP ๋ถ€๋ชจ ๋ทฐ์—์„œ ์œ„์ชฝ์— ์œ„์น˜์‹œํ‚ด

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="R | T"
        android:gravity="right|top"
        android:layout_gravity="right"/>
๊ฒ‰(๋‚ด์šฉ๋ฌผ ์œ„์น˜) : gravity,
์†(๋ถ€๋ชจ ๋ทฐ): layout_gravity
left center_horizontal right
top left center_horizontal right
center_vertical center_vertical center ( gravity : x )  center_vertical | right
bottom bottom center_horizontal | bottom right | bottom

        //์—ด ๋จผ์ €, ๊ทธ ๋’ค ํ–‰ (ex. left|top)

    @@@ frame ์•„๋‹๋•Œ ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€ ํ™•์ธ @@@

 

4. ์Šคํƒ€์ผ ๋ฐ ํ…Œ๋งˆ

    1) ์Šคํƒ€์ผ : view ๋˜๋Š” ์ฐฝ์˜ ๋ชจ์–‘๊ณผ ํ˜•์‹์„ ์ง€์ •ํ•˜๋Š” ์†์„ฑ ๋ชจ์Œ (๋†’์ด, ํŒจ๋”ฉ, ๊ธ€๊ผด ์ƒ‰์ƒ, ํฌ๊ธฐ, ๋ฐฐ๊ฒฝ์ƒ‰ ๋“ฑ ์ง€์ •)

    - ๋ ˆ์ด์•„์›ƒ์„ ์ง€์ •ํ•˜๋Š” xml๊ณผ ๋ณ„๊ฐœ์ธ xml ๋ฆฌ์†Œ์Šค์— ์ •์˜๋จ

    - ์•ˆ๋“œ๋กœ์ด๋“œ์˜ ์Šคํƒ€์ผ์€ ์›น ๋””์ž์ธ์˜ cascading ์Šคํƒ€์ผ ์‹œํŠธ์™€ ์œ ์‚ฌํ•จ -> ์ฝ˜ํ…์ธ ์™€ ๋””์ž์ธ์„ ๊ตฌ๋ถ„ ๊ฐ€๋Šฅ

res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <style name="RedTextView" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width"> wrap_content</item>
        <item name="android:layout_height"> wrap_content</item>
        <item name="android:textColor">#FF0000</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">24sp</item>
    </style>
</resources>

@@@@

 

    2) ํ…Œ๋งˆ : ๊ฐœ๋ณ„ View๊ฐ€ ์•„๋‹ˆ๋ผ ์ „์ฒด Activity ๋˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ ์šฉ๋˜๋Š” ์Šคํƒ€์ผ

    - ์Šคํƒ€์ผ์ด ํ…Œ๋งˆ๋กœ ์ ์šฉ๋œ ๊ฒฝ์šฐ ์•กํ‹ฐ๋น„ํ‹ฐ ๋˜๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํฌํ•จ๋œ ๋ชจ๋“  ๋ทฐ๊ฐ€ ์ž์‹ ์ด ์ง€์›ํ•˜๋Š” ๊ฐ ์Šคํƒ€์ผ ์†์„ฑ์„ ์ ์šฉํ•จ
        (ex. ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ํ…Œ๋งˆ์™€ ๋™์ผํ•œ CodeFont ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ๋Ÿฌ๋ฉด ํ•ด๋‹น ์•กํ‹ฐ๋น„ํ‹ฐ์— ํฌํ•จ๋œ ๋ชจ๋“  ํ…์ŠคํŠธ์— ๋…น์ƒ‰์˜ ๊ณ ์ • ํญ ๊ธ€๊ผด์ด ์ ์šฉ๋จ)

    <Button
        style="@style/RedTextView"
        android:text="L | T"
        android:gravity="left|top"
        android:layout_gravity="left"/>

 

@@์Šคํƒ€์ผ์ด๋ž‘ ํ…Œ๋งˆ๋Š” ๋ณ„๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•œ๋ฒˆ์— ์“ฐ๋Š” ๊ฑฐ? style="@style/์–ด์ฉŒ๊ตฌ " ์ด๋ ‡๊ฒŒ ๋„ฃ๋Š” ๊ฒŒ ํ…Œ๋งˆ์ธ๊ฐ€? @@

 

์‹ค์Šต

<?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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" 		//0์œผ๋กœ ํ•ด์•ผ ํ•จ
        android:layout_weight="1"
        android:orientation="vertical" 		//๊ฒฐ๊ตญ ์„ธ๋กœ๋‹ˆ๊นŒ
        android:background="@color/black">

        <Button
            android:id="@+id/btn11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BUTTON1"/>
        <Button
            android:id="@+id/btn12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BUTTON2"
            android:layout_gravity="center"/> 		//์ˆ˜์ง ์•„๋ž˜์นธ ๊ฐ€์šด๋ฐ
        <Button
            android:id="@+id/btn13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BUTTON3"
            android:layout_gravity="right"/> 		//์ˆ˜์ง ์•„๋ž˜์นธ ์˜ค๋ฅธ์ชฝ
    
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="10dp" 		//ํ…Œ๋‘๋ฆฌ ๋งˆ์ง„์œผ๋กœ ์ฒ˜๋ฆฌ. ํ…Œ๋‘๋ฆฌ ์ƒ‰ ๋„ฃ์œผ๋ ค๋ฉด 
        android:background="@android:color/holo_green_light"> //์ฒซ(๋ถ€๋ชจ?) LinearLayout์—์„œ background ์ƒ‰ ๋ฐ”๊พธ๊ธฐ
        <Button
            android:id="@+id/btn21"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/btn22" 	//๊ธฐ์ค€ ๋‘๊ณ  ์˜†. ๋งˆ์ง„ ๋‘˜๊ฑฐ๋ฉด layout_marginRight="20dp"
            android:text="BUTTON4"/>
        <Button
            android:id="@+id/btn22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_alignParentRight="true" 		//์šฐ์ธก์œผ๋กœ ๋ถ™์ž„
            android:text="BUTTON5"/>
        <Button
            android:id="@+id/btn23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right" 			//?? ์ด๊ฑด๋ญ์ง€
            android:layout_marginLeft="10dp" 		//์˜†์ด๋ž‘ 10 ๋„์›€
            android:layout_below="@id/btn21" 		//๋ฐ‘
            android:text="BUTTON6"/>
    </RelativeLayout>
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/holo_purple"
        android:oriention="vertical">

        <Button
            android:id="@+id/btn31"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BUTTON7"
            app:layout_constraintBottom_toBottomOf="parent" 		//์ค‘์•™์œผ๋กœ ๋ฐฐ์น˜
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
        <Button
            android:id="@+id/btn32"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" 		//์ค‘๊ฐ„ ์œ„์น˜
            android:text="BUTTON8"
            android:layout_marginLeft="20dp"
            app:layout_constraintStart_toEndOf="@id/btn31" 		//์‹œ์ž‘์ด 7๋ฒˆ์˜ ๋
            app:layout_constraintBottom_toBottomOf="@id/btn31"
            app:layout_constraintTop_toTopOf="@id/btn31"/>
        <Button
            android:id="@+id/btn33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="BUTTON9"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/btn31"/>
        
    </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>