LoginSignup
3
2

More than 3 years have passed since last update.

[Android] ダイアログの横のスペース [メモ]

Posted at

ダイアログに苦しめられたのでメモ。

起こった事


カスタムレイアウトのダイアログを作る時に、この両端のマージンがどうしてもいい感じに指定できなくて苦しめられた。

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="ダイアログ"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
class TestDialog : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return requireActivity().let {
            val builder = AlertDialog.Builder(it)
            val inflater = requireActivity().layoutInflater

            builder.setView(inflater.inflate(R.layout.fragment_dialog, null))
            builder.create()
        }
    }
}

実装したコードは上記です。
やりたいことはダイアログの左右のマージンを100dpに指定したい。

解決方法

まず、ダイアログの横目一杯に表示してみようとしました。


    override fun onStart() {
        super.onStart()
        dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    }

完全に横に広げることができません。。。。

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="100dp"
    >

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:background="@color/white" <- 追加
        android:text="ダイアログ"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
 override fun onStart() {
        super.onStart()
        dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)

広げたはいいもののここから、何やればいいかわからなくなったw
とりあえず、先程に戻って最後に残ったマージンがどこで決まってるか突き詰めることができました!何やらダイアログはデフォルトで左右上下にinsetが設定されているみたいです!

なので、スタイルの設定してあげれば行けそう。

styles.xml
<style name="CustomDialog" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:windowBackground">@drawable/background_dialog</item>
    </style>
backgroud_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<inset
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="0dp"
    android:insetLeft="100dp"
    android:insetRight="100dp"
    android:insetTop="0dp"
    >
    <shape android:shape="rectangle">
        <solid android:color="@color/white" />
    </shape>
</inset>
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return requireActivity().let {
            val builder = AlertDialog.Builder(it, R.style.CustomDialog)

できた?
ためしに横のInsetを0dpにしてみる

dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)とか設定しなくても目一杯広がってくれてますね。

3
2
1

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
3
2