APP

[์…ฐ์ดํ”„ ๋“œ๋กœ์–ด๋ธ”(ShapeDrawable)] ๋ฐฐ๊ฒฝ๋ณ€๊ฒฝ/ํ…Œ๋‘๋ฆฌ๋งŒ ์žˆ๋Š” ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

kite707 2021. 7. 12.

https://sectumsempra.tistory.com/75

 

[์ƒํƒœ ๋“œ๋กœ์–ด๋ธ”(StateListDrawable)] ์ด๋ฏธ์ง€ ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ ๋ฐ ์ด๋ฏธ์ง€ ๋ฐ”๊พธ๊ธฐ

๋ฒ„ํŠผ์˜ background ์†์„ฑ์„ ์ด๋ฏธ์ง€๋กœ ์„ค์ •ํ•˜๋ฉด ์ด๋ฏธ์ง€๋Š” ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ด๋ฏธ์ง€๊ฐ€ ๋ฐ”๋€Œ๋Š” ๋ฒ„ํŠผ์„ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋“œ๋กœ์–ด๋ธ”(Drawable)์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. Drawable์ด๋ž€

sectumsempra.tistory.com

์•ž ๊ธ€๊ณผ ์ด์–ด์ง€๋Š” ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

drawable ํด๋” ์•„๋ž˜์— ์ƒˆ๋กœ์šด layoutํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜์ž.

 

bbb.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    //ํฌ๊ธฐ ์„ค์ •
    <size android:width="200dp" android:height="120dp"/>
    //ํ…Œ๋‘๋ฆฌ ์„ค์ •(์„  ๊ตต๊ธฐ/์ƒ‰๊น”)
    <stroke android:width="1dp" android:color="#0000ff"/>
    //์ฑ„์šฐ๊ธฐ ์ƒ‰ ์„ค์ •
    <solid android:color="#aabbff"/>
    //์•„๋ž˜ ์—ฌ๋ฐฑ
    <padding android:bottom="1dp"/>

</shape>

์•ž๊ณผ ๊ฐ™์ด ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฐฐ๊ฒฝ์„ ์„ค์ •ํ•œ๋‹ค.

์„ค์ • ๊ฐ’๊ณผ ์†Œ์Šค ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bbb"
        android:text="Button"
        app:backgroundTint="@android:color/transparent"
        app:backgroundTintMode="add"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

๋ฐฐ๊ฒฝ ๋ณ€๊ฒฝํ•˜๊ธฐ

 

์ดํ›„ ๋ฐฐ๊ฒฝ์ƒ‰์„ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด drawable ํด๋” ์•„๋ž˜์— background.xmlํŒŒ์ผ์„ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค. ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#7288DB"
        android:centerColor="#3250B4"
        android:endColor="#254095"
        android:angle="90"
        android:centerY="0.5"/>
    
    <corners android:radius="2dp"/>

</shape>

๊ทธ๋ฆฌ๊ณ  activity_main์—์„œ ๋ฐฐ๊ฒฝ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•œ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@drawable/background">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bbb"
        android:text="Button"
        app:backgroundTint="@android:color/transparent"
        app:backgroundTintMode="add"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>

ํ…Œ๋‘๋ฆฌ๋งŒ ์žˆ๋Š” ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

btn.xmlํŒŒ์ผ์„ drawable ํด๋” ์•„๋ž˜์— ๋งŒ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

<layer-list>ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ทธ๋ž˜ํ”ฝ์„ ํ•˜๋‚˜์˜ XMLํŒŒ์ผ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

btn.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#BE55DA"/>
            <solid android:color="#00000000"/>
            <size android:width="200dp" android:height="100dp"/>
        </shape>
    </item>

    <item android:top="1dp" android:bottom="1dp"
        android:right="1dp" android:left="1dp">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#FF55DA"/>
            <solid android:color="#00000000"/>
        </shape>
    </item>

</layer-list>

์œ„์—์„œ ํ–ˆ๋˜๋Œ€๋กœ ๋ฒ„ํŠผ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด btn2.xml์„ background์— ์ง€์ •ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@drawable/background">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bbb"
        android:text="Button"
        app:backgroundTint="@android:color/transparent"
        app:backgroundTintMode="add"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn2"
        android:text="Button"
        app:backgroundTint="@android:color/transparent"
        app:backgroundTintMode="add"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="170dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

๋Œ“๊ธ€