LoginSignup
0
0

More than 3 years have passed since last update.

DialogFragmentを角丸にした部分に背景色が残ってしまうときの対処法

Posted at

毎回これにハマります。。

角丸にする一般的な方法としては

corner_radius.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid
                    android:color="@color/colorBackground"
                    />
            <corners
                    android:radius="16dp"
                    />
        </shape>
    </item>
</selector>

このdrawableをbackgroundに指定すると角丸になる、というのは定石かと思います。
しかし、DialogFragmentに限ってこれがうまくいかず色が残ってしまう

↓問題となったコードです。

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        super.onCreateDialog(savedInstanceState)


        val dialog = AlertDialog.Builder(requireContext()).also {
            it.setView(R.layout.fragment_some_dialog)
        }
        return dialog.create()
    }

ちなみにこれは純粋に背景色を透過させれば直るのですが、xml側で指定してもうまくいかないんですよね。
AlertDialogを使うのをやめてDialogを使って、背景色を透明に指定したらうまくいきました。

   override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        super.onCreateDialog(savedInstanceState)

        return Dialog(requireContext()).also {
            it.setContentView(R.layout.fragment_test_call_dialog)
            it.window?.setBackgroundDrawableResource(R.color.zs_color_transparent)
        }
    }

参考:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0