微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自定义对话框未与某些 XML 一起显示

如何解决自定义对话框未与某些 XML 一起显示

我正在尝试显示自定义对话框,但它没有显示,而是使背景透明。最奇怪的是,当我只更改自定义对话框 xml 时,它显示没有问题,所以我认为它可能是 xml,但我一直在寻找一段时间,但我找不到问题。

这是片段代码

class DeleteConfirmationFragment : Fragment() { 
private lateinit var binding: FragmentDeleteConfirmationBinding
lateinit var dialog : Dialog

  override fun onCreateView(
        inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_delete_confirmation,container,false)
    }

 override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        super.onViewCreated(view,savedInstanceState)
        binding = FragmentDeleteConfirmationBinding.bind(view)

        dialog = context?.let { Dialog(it) }!!
        binding.btnNo.setonClickListener {
            openDeletedAccountDialog()
        }
    }

    private fun openDeletedAccountDialog() {
        dialog.setContentView(R.layout.dialog_deleted_account) // if i change this xml file for other it 
        //works properly so i think it Could be an android bug..

        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.show()
    }
}

这是不起作用的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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp">


    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewDeletedAccount"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:cardCornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                android:id="@+id/tvDeletedAccount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:fontFamily="@font/poppins_semibold"
                android:text="Cuenta eliminada"
                android:textColor="@color/black"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.496"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:fontFamily="@font/poppins_semibold"
                android:gravity="center_horizontal"
                android:padding="10dp"
                android:text="Tu cuenta se ha eliminado correctamente.  ¡Esperamos volver a verte pronto!"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvDeletedAccount"
                app:layout_constraintVertical_bias="0.0" />


        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

使用无效的自定义 xml 显示

enter image description here

其他正常工作的自定义对话框 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="wrap_content"
    android:layout_gravity="center"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp">

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/poppins_bold"
        android:gravity="center"
        android:text="¡Tu friendzone ha crecido!"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/cardViewFriendzone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.884" />

    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewFriendzone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <androidx.cardview.widget.CardView
                android:id="@+id/cardView2"
                android:layout_width="100dp"
                android:layout_height="100dp"
                app:cardCornerRadius="100dp"
                app:cardElevation="20dp"
                android:layout_marginTop="20dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.47"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <ImageView
                    android:id="@+id/tvNotificationImage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="centerCrop"
                    app:srcCompat="@drawable/pedra" />
            </androidx.cardview.widget.CardView>

            <TextView
                android:id="@+id/tvDialogName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:fontFamily="@font/poppins_semibold"
                android:text="María González"
                android:textColor="@color/black"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.496"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardView2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:fontFamily="@font/poppins_semibold"
                android:padding="10dp"
                android:text="Soy una chica aventurera,amistosa y muy curiosa. Mi pasión son los animales,en especial los perros."
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvDialogName"
                app:layout_constraintVertical_bias="0.45" />


        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

如果我像这样在打开的已删除对话框功能中只更改 xml,它会正确显示一个对话框:

    private fun openDeletedAccountDialog() {
        dialog.setContentView(R.layout.dialog_friendzone_layout) // <-- change made here
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.show()
    }

使用其他自定义对话框 xml 显示

enter image description here

知道发生了什么吗?

解决方法

我也遇到了同样的问题,这是因为我在 0dp 中使用了 ConstrintLayout 以使用 match_constraint 大小,但它在对话框中不起作用。

,

您只需将 android:layout_width="0dp"CardView 更改为 android:layout_width="match_parent"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。