【问题标题】:How to set custom layout for AlertDialog?如何为 AlertDialog 设置自定义布局?
【发布时间】:2020-06-17 14:10:37
【问题描述】:

调用我的对话框:

alertDialog = showInfoDialog(message = "$wrongPasscodeMessage\n$retryMessage")

方法如下:

fun FragmentActivity.showInfoDialog(message: String?): AlertDialog? {
    return try {
        val customLayout = layoutInflater.inflate(R.layout.custom_layout, null)
        AlertDialog.Builder(this)
            .setView(customLayout)
            .setMessage(message)
            .setCancelable(false)
            .show()
    } catch (e: java.lang.Exception) {
        e.log()
        null
    }
}

这是我的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@color/red"
    android:paddingStart="@dimen/dp_44"
    android:paddingEnd="@dimen/dp_44"
    app:cardCornerRadius="@dimen/dp_12" />

但我不知道为什么我的对话框没有背景红色和圆角? 我错过了什么?

更新: 例如,如果我更改我的 custom_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/red" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="350dp"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:src="@drawable/googleg_disabled_color_18"/>
</LinearLayout>

我是这样的:

但我想要这样的东西

【问题讨论】:

    标签: android kotlin android-alertdialog material-components-android material-components


    【解决方案1】:

    按照以下内容创建可绘制的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="20dp" />
    </shape>
    

    形状中的半径为您提供圆角。现在您可以将此可绘制对象设置为您的LinearLayout 对话框的背景,或者在您的res/styles.xml 中使用自定义&lt;style&gt;

    <style name="CustomAlertDialog" parent="Theme.AppCompat.DayNight.Dialog.Alert">
            <item name="android:windowBackground">@drawable/dialog_background</item>
            <item name="textAllCaps">false</item>
    </style>
    

    并在您的 AlertDialog.Builder 中使用该样式:

    AlertDialog.Builder(this, R.style.CustomAlertDialog).setView(...
    

    使用该样式,您可以轻松地在应用内的所有对话框中一致地使用该形状。

    【讨论】:

      【解决方案2】:

      您不需要CardView 来实现带圆角的AlertDialog: 只需使用标准构造函数:

       new MaterialAlertDialogBuilder(this, R.style.DialogOverlay)
                      .setMessage("Wrong Passcode\nTry again in 5 minutes")
                      .show();
      

      使用自定义主题覆盖以使消息居中:

      <style name="DialogOverlay" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
          <item name="materialAlertDialogBodyTextStyle">@style/BodyText</item>    
      </style>
      
      <style name="BodyText" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
          <item name="android:gravity">center_horizontal</item>
      </style>
      

      如果您想更改圆角,只需在主题叠加层中添加 shapeAppearanceOverlay

      <style name="DialogOverlay" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
          ...
          <item name="shapeAppearanceOverlay">@style/DialogCorners</item>    
      </style>
      
      <style name="DialogCorners">
          <item name="cornerFamily">rounded</item>
          <item name="cornerSize">12dp</item>
      </style>
      

      【讨论】:

      • 不错的解决方案,但我得到的对话框没有圆角,但我添加了你写的所有内容。
      • @Morozov 您使用的是哪个版本的 Material 组件,您的应用主题是什么? Material 组件库需要一个 Material Components 主题。
      • Theme.AppCompat.DayNight.NoActionBar,但是对于另一个屏幕,我使用了Theme.MaterialComponents.DayNight.NoActionBar
      • @Morozov 将Theme.MaterialComponents.DayNight.NoActionBar 用于ActivityAlertDialog
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 2019-03-08
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多