制作圆形自定义警报对话框:着色区域不正确

理查德

我已成功尝试使用以下代码制作自定义警报对话框。然而,当我试图使Constraint Layoutcustom_dialog.xml有圆角,它表现出了故障:黑色(黑暗/有色)区不包括那些对圆角由于白色的区域。这就是我要说的:

自定义对话框

我的代码MainActivity.kt

package com.example.customdialog

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.custom_dialog.view.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun showDialog(view: View) {
        val mDialogView = LayoutInflater.from(this).inflate(R.layout.custom_dialog, null)
        val mBuilder = AlertDialog.Builder(this)
            .setView(mDialogView)

        val mAlertDialog = mBuilder.show()
        mDialogView.closeDialog.setOnClickListener {
            mAlertDialog.dismiss()
        }
    }
}

我的代码custom_dialog.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:background="@drawable/rounded_corners">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="141dp"
        android:layout_marginLeft="141dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="142dp"
        android:layout_marginRight="142dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:src="@mipmap/smiley_foreground"
        android:tint="#FFF"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="WORKED!"
        android:textColor="#FFF"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Apparently, the custom dialog worked"
        android:textColor="#FFF"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <Button
        android:id="@+id/closeDialog"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/rounded_button"
        android:backgroundTint="#FFF"
        android:text="Close Dialog"
        android:textColor="@color/colorPrimary"
        android:textSize="18sp"
        android:textStyle="bold"
        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/textView3" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的代码使Constraint Layoutfromcustom_dialog.xml四舍五入:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorPrimary"/>
    <corners android:radius="25dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

如何删除对话框周围的白色区域?

迷你芯片

我有同样的问题。使背景透明可以解决这个问题。

<style name="Theme_Dialog" parent="Theme.AppCompat.Light.Dialog">

  <item name="android:background">@android:color/transparent</item>
</style>

在初始化对话框时添加此样式。

val dialog = Dialog(context, R.style.Theme_Dialog)

不确定你的@drawable/rounded_corners”。相反,你可以有卡片视图并将这个约束布局放在卡片视图中。使用 app:cardCornerRadius="@dimen/space_18" 你已经根据需要设置了半径。

<androidx.cardview.widget.CardView 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="match_parent"
xmlns:toots="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="@color/white_color"
app:cardCornerRadius="@dimen/space_18">

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

  ..

</androidx.constraintlayout.widget.ConstraintLayout>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何制作自定义警报对话框全屏

如何关闭“自定义警报”对话框

Android中的“自定义警报”对话框

如何自定义警报对话框,以使按钮适合警报对话框

带有动态布局的自定义对话框显示不正确

Flutter-自定义警报对话框未显示

在服务android中显示自定义警报对话框

Android-如何在自定义警报对话框中检查按钮单击?

通过android自定义警报对话框传递额外数据

在自定义警报对话框中使用时 ExpandableListView 对象为 null

具有RecyclerView的自定义警报对话框

自定义警报对话框不会关闭

如何在自定义警报对话框中刷新内容/ UI

如何在Android中使用网格视图创建自定义警报对话框?

具有多个EditText的自定义警报对话框

两个活动的一个自定义警报对话框

在自定义警报对话框中获取单选按钮的值

在自定义警报对话框中从 Edittext 检索数据时显示空值?

自定义警报对话框搜索项目结果错误

自定义警报对话框的背景色

从Android中的ListView适配器启动自定义警报对话框

具有多个视图的自定义警报对话框

实现自定义弹出窗口的正确方法?活动与对话框与警报对话框

自定义对话框警报

使 Multiautocompletetextview 在警报对话框中添加按钮上方可滚动,我想要这种类型的自定义警报对话框

如何将自定义警报对话框中的项目添加到列表视图?

如何在自定义警报对话框中将信息从活动传递到编辑文本

在自定义警报对话框上使用约束布局

对话框宽度不正确