MaterialAlertDialog 主题在 ComponentActivity 上损坏

问题描述 投票:0回答:1

我目前正在开发一个 Android 应用程序,该应用程序主要使用用于视图的 XML 框架编写,并且我目前正在其中添加一个基于 Jetpack Compose 的屏幕。

在我的项目中,我使用自定义布局来显示自定义

MaterialAlertDialog
.

这里的布局:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
  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"
  style="@style/CustomCardView"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:cardCornerRadius="16dp"
  >

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

    <TextView
      android:id="@+id/title"
      style="@style/TextViewRegular.DialogTitle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      app:layout_constraintBottom_toTopOf="@id/btn"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      tools:text="@string/dialogMessage_newGridCompleted"
      />

    <Button
      android:id="@+id/btn"
      style="@style/DialogButtonOk"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="32dp"
      android:text="@android:string/ok"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@id/title"
      />

  </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

在此布局中使用的样式:

  <style name="CustomCardView"
    parent="Widget.MaterialComponents.CardView">
    <item name="cardBackgroundColor">@color/cardBackground</item>
  </style>
  <style name="TextViewRegular"
    parent="Widget.MaterialComponents.TextView">
    <item name="android:textColor">@color/white</item>
    <item name="fontFamily">@font/avenir_next_regular</item>
    <item name="android:fontFamily">@font/avenir_next_regular</item>
    <item name="android:textSize">19sp</item>
  </style>

  <style name="TextViewRegular.DialogTitle">
    <item name="android:textColor">@color/background</item>
    <item name="android:textSize">19sp</item>
  </style>
  <style name="DialogButtonOk"
    parent="Widget.MaterialComponents.Button">
    <item name="android:textColor">@color/white</item>
    <item name="fontFamily">@font/avenir_next_regular</item>
    <item name="android:fontFamily">@font/avenir_next_regular</item>
    <item name="android:gravity">center</item>
    <item name="android:padding">16dp</item>
    <item name="android:textSize">14sp</item>
    <item name="strokeWidth">1dp</item>
    <item name="strokeColor">@color/white</item>
    <item name="backgroundTint">@color/btnBackground</item>
    <item name="rippleColor">@color/white</item>
    <item name="cornerRadius">5.5dp</item>
  </style>

现在,我用来显示警报对话框的代码:

private fun displayDialog(@StringRes messageRes: Int)
{
    val customView = LayoutInflater.from(this).inflate(R.layout.dialog_simple_btn, null, false)

    customView.findViewById<TextView>(R.id.title)?.setText(messageRes)

    val dialog = MaterialAlertDialogBuilder(this).apply {
      setView(customView)
      setCancelable(false)
    }.create()


    dialog?.apply {
      window?.setBackgroundDrawableResource(R.drawable.bg_dialog)
      show()
    }

    customView.findViewById<Button>(R.id.btn)?.setOnClickListener {
      dialog?.dismiss()
    }
  }

这里是可绘制的 bg_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle"
  >
  <solid android:color="@android:color/transparent" />
  <corners android:radius="16dp" />
  <padding
    android:left="16dp"
    android:right="16dp"
    />
</shape>

AppCompatActivity
中使用时,结果是预期的:

但是在

ComponentActivity
中使用时,按钮的样式就坏了。如您所见,按钮上缺少背景。涟漪效应也不见了。

你知道问题是什么吗?

android material-design android-jetpack-compose material-components-android
1个回答
0
投票

感谢@Abdulrahman Bahaml 的评论,我发现了 Jetpack 组件

Dialog
。我用它来为撰写屏幕重新创建我的自定义对话框。

这里是结果:

© www.soinside.com 2019 - 2024. All rights reserved.