首页 > 解决方案 > 如何很好地使用 MaterialAlertDialogBu​​ilder?

问题描述

当我使用 dialog.builder 时,字体大小是正确的,但是当我使用 MaterialAlertDialogBu​​ilder 时,正文的字体大小更小。没关系?

implementation 'com.google.android.material:material:1.1.0-alpha06'

我用这个主题

<style name="AppTheme" parent="Theme.MaterialComponents.Light">

材质组件代码

MaterialAlertDialogBuilder(this)
    .setMessage("This is a test of MaterialAlertDialogBuilder")
    .setPositiveButton("Ok", null)
    .show()

截图_20190512-115103_2

警报对话框生成器

AlertDialog.Builder(this)
            .setMessage("This is a test of AlertDialog.Builder")
            .setPositiveButton("Ok", null)
            .show()

截图_20190512-115241_2

问题出在哪里?

标签: androidandroid-alertdialogmaterial-components-androidmaterial-components

解决方案


这是故意的。他们使用不同的风格。

您可以使用以下方式更改它:

  <style name="Body_ThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="materialAlertDialogBodyTextStyle">@style/BodyMaterialAlertDialog.MaterialComponents.Body.Text</item>
  </style>
  <style name="BodyMaterialAlertDialog.MaterialComponents.Body.Text" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
    <item name="android:textColor">@color/colorAccent</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">bold</item>
  </style>

接着:

 new MaterialAlertDialogBuilder(this,
      R.style.Body_ThemeOverlay_MaterialComponents_MaterialAlertDialog)
            .setTitle("Title")
            .setMessage("Message......")
            ...
            .show();

在此处输入图像描述在此处输入图像描述


推荐阅读