10
02

๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋งŒ๋“ค๋˜ ์ค‘, ๋‹ค์ด์–ผ๋กœ๊ทธ ๋ชจ์„œ๋ฆฌ๋ฅผ ๋‘ฅ๊ธ€๊ฒŒ ํ•ด์•ผ๋๋‹ค.

๋‹น์—ฐํžˆ ๋  ๊ฒƒ์ด๋ผ๋Š” ์ƒ๊ฐ์œผ๋กœ ๊ฐ€์žฅ ๋ฐ”๊นฅ ๋ ˆ์ด์•„์›ƒ์— ์ด๋ ‡๊ฒŒ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ,

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:cardCornerRadius="20dp">
 

 

์ด๋Ÿฐ์‹์œผ๋กœ ๋‚ด๊ฐ€ ์ง€์ •ํ•œ ๋ฐฐ๊ฒฝ ๋ฐ”๊นฅ์—๋„ ๋˜ ๋ฐฐ๊ฒฝ์ด ์‚ด์•„์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค. ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณธ ๊ฒฐ๊ณผ, ์ฝ”๋“œ์—์„œ ์ง์ ‘ ์ง€์ •ํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ๋˜๋Š” ๋ฌธ์ œ์˜€๋Š”๋ฐ

AlertDialog ์ธ์Šคํ„ด์Šค์— ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์†์„ฑ์„ ์ง€์ •ํ•ด์ฃผ๋ฉด๋œ๋‹ค.

alertDialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
 

์ด ๋‘ ์‚ฌ์ง„์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋Š”๊ฑด, layout์— ์ •์˜๋œ background๋ง๊ณ  background๊ฐ€ ๋˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ, ์ด์™€ ๋น„์Šทํ•œ ์˜ˆ๋ฅผ ๋“ค์ž๋ฉด, Theme์ˆ˜์ •์„ ํ–ˆ๋Š”๋ฐ Button์˜ ์Šคํƒ€์ผ์ด ์•ˆ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ฒŒ์จ ๋ˆˆ์น˜ ์ฑˆ ์‚ฌ๋žŒ์ด ์žˆ์„์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์ด ๋ฌธ์ œ๋Š” AlertDialog๊ฐ€ ๊ฐ–๋Š” Style์ด ๊ธฐ๋ณธ์œผ๋กœ ์ง€์ •๋˜์–ด ์žˆ์–ด์„œ ์ƒ๊ธฐ๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

AlertDialog๋ฅผ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์™€ ๋ณด๊ฒ ๋‹ค.

private fun initDialog(binding: ViewDataBinding) {
        val dialogBuilder = AlertDialog.Builder(context)
            .setView(binding.root)
            .setCancelable(false)

        alertDialog = dialogBuilder.create()
        alertDialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        alertDialog.show()
    }
 

AlertDialog API์˜ Builder๋ฅผ ์ด์šฉํ•ด ๋งŒ๋“ค์–ด์ง€๊ณ , setView์—์„œ ์ง€์ •ํ•ด๋‘” ๋ ˆ์ด์•„์›ƒํŒŒ์ผ๊ณผ ์—ฐ๊ฒฐํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์œ„ ๋ฌธ์ œ๋ฅผ ์•Œ์•„์ฐจ๋ฆด ์ˆ˜ ์—†๋Š”๋ฐ, AlertDialog๋กœ ๋“ค์–ด๊ฐ€๋ณด๋ฉด ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

    /**
     * Construct an AlertDialog that uses an explicit theme.  The actual style
     * that an AlertDialog uses is a private implementation, however you can
     * here supply either the name of an attribute in the theme from which
     * to get the dialog's style (such as {@link R.attr#alertDialogTheme}.
     */
    protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, resolveDialogTheme(context, themeResId));
        mAlert = new AlertController(getContext(), this, getWindow());
    }
 

์—ฌ๋Ÿฌ ์ƒ์„ฑ์ž ์ค‘ ํ•˜๋‚˜์ธ๋ฐ, ์ƒ์„ฑํ•  ๋•Œ Theme์„ ๊ฐ€์ ธ์˜จ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์„œ ๊ฐ€์ ธ์˜ค๋Š” Theme์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

public static final int Theme_Material_Dialog_Alert
 

์ด๊ฒƒ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋˜ ๊ฒƒ์ด๊ณ (Dark, Light mode ๋ชจ๋‘ ์ง€์›ํ•˜๊ณ  ์žˆ๋‹ค.) ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ปค์Šคํ…€ ๋‹ค์ด์–ผ๋กœ๊ทธ ํ…Œ๋งˆ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋˜์ง€, ์•„์˜ˆ ๋ฐฐ๊ฒฝ์„ ํˆฌ๋ช…์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ alertDialogTheme ์†์„ฑ์„ ๋”ฐ๋ผ๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ํ…Œ๋งˆ์—์„œ ์ง€์ •ํ•ด๋‘๋ฉด ๋œ๋‹ค.

์ปค์Šคํ…€ ํ…Œ๋งˆ๋Š” values/theme.xmlํŒŒ์ผ์—์„œ ์†๋ณด๋ฉด๋˜๋Š”๋ฐ,

<style name="Theme.ShyPolarBear" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="alertDialogTheme">@style/CustomAlertDialog</item>
</style>
<style name="CustomAlertDialog" parent="Theme.AppCompat.Dialog.Alert">
        <item name="colorAccent">@color/Black_01</item>
        <item name="android:windowBackground">@android:color/transparent</item>
</style>
 
์ด๋Ÿฐ์‹์œผ๋กœ ๋ฏธ๋ฆฌ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ณ , ๋‹ค์ด์–ผ๋กœ๊ทธ ์šฉ ๋ ˆ์ด์•„์›ƒ์„ ๋”ฐ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์œ„ ๋ฐฉ๋ฒ• ์ฒ˜๋Ÿผ ์ฝ”๋“œ๋กœ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒŒ ๋” ํŽธํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณด์ธ๋‹ค.

ํ•ด๋‹น parent ํ…Œ๋งˆ๋ฅผ ํƒ€๊ณ ํƒ€๊ณ  ๋“ค์–ด๊ฐ€๋ฉด Base.Theme.AppCompat๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ ์—ฌ๊ธฐ ์˜ฌ๋ ค๋‘˜ ํ…Œ๋‹ˆ ํ•„์š”ํ•œ ์†์„ฑ์„ overrideํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค. ๊ผญ ์•„๋ž˜ ์†์„ฑ์— ์žˆ๋Š” ๊ฒƒ๋งŒ ์ง€์ •ํ•  ํ•„์š” ์—†์ด ๊ทธ๋ƒฅ ๊ธฐ์กด Theme์—๋„ ์‚ฌ์šฉํ•˜๋Š” colorAccent์™€ ๊ฐ™์€ ์†์„ฑ์„ ์ง€์ •ํ•˜๋ฉด ์ง€์ •ํ•œ ์†์„ฑ์€ ํ•ด๋‹น AlertDialog์—์„œ๋งŒ ์ ์šฉ๋œ๋‹ค๋Š” ์ ๋„ ๊ธฐ์–ตํ•˜์ž.

<style name="Base.V7.Theme.AppCompat.Dialog" parent="Base.Theme.AppCompat">
        <item name="android:colorBackground">?attr/colorBackgroundFloating</item>
        <item name="android:colorBackgroundCacheHint">@null</item>

        <item name="android:windowFrame">@null</item>
        <item name="android:windowTitleStyle">@style/RtlOverlay.DialogWindowTitle.AppCompat</item>
        <item name="android:windowTitleBackgroundStyle">@style/Base.DialogWindowTitleBackground.AppCompat</item>
        <item name="android:windowBackground">@drawable/abc_dialog_material_background</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@style/Animation.AppCompat.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>

        <item name="windowActionBar">false</item>
        <item name="windowActionModeOverlay">true</item>

        <item name="listPreferredItemPaddingLeft">24dip</item>
        <item name="listPreferredItemPaddingRight">24dip</item>

        <item name="android:listDivider">@null</item>

        <item name="android:buttonBarStyle">@style/Widget.AppCompat.ButtonBar.AlertDialog</item>
        <item name="android:borderlessButtonStyle">@style/Widget.AppCompat.Button.Borderless</item>
        <item name="android:windowCloseOnTouchOutside">true</item>
    </style>
 

 

์ฐธ์กฐ:

https://developer.android.com/reference/android/R.style#Theme_Material_Dialog_Alert

"๋Œ“๊ธ€, ๊ณต๊ฐ ๋ฒ„ํŠผ ํ•œ ๋ฒˆ์”ฉ ๋ˆ„๋ฅด๊ณ  ๊ฐ€์ฃผ์‹œ๋ฉด ํฐ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค"
๋ฐ˜์‘ํ˜•
COMMENT