如何使用 MaterialAlertDialogBu​​ilder 好?

Rul*_*lan 7 android android-alertdialog material-components material-components-android

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

implementation 'com.google.android.material:material:1.1.0-alpha06'
Run Code Online (Sandbox Code Playgroud)

我用这个主题

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
Run Code Online (Sandbox Code Playgroud)

材料组件代码

MaterialAlertDialogBuilder(this)
    .setMessage("This is a test of MaterialAlertDialogBuilder")
    .setPositiveButton("Ok", null)
    .show()
Run Code Online (Sandbox Code Playgroud)

截图_20190512-115103_2

AlertDialog.Builder

AlertDialog.Builder(this)
            .setMessage("This is a test of AlertDialog.Builder")
            .setPositiveButton("Ok", null)
            .show()
Run Code Online (Sandbox Code Playgroud)

截图_20190512-115241_2

问题出在哪儿?

Gab*_*tti 7

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

您可以使用以下内容更改它:

  <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>
Run Code Online (Sandbox Code Playgroud)

进而:

 new MaterialAlertDialogBuilder(this,
      R.style.Body_ThemeOverlay_MaterialComponents_MaterialAlertDialog)
            .setTitle("Title")
            .setMessage("Message......")
            ...
            .show();
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明在此处输入图片说明


小智 2

你可以这样解决:

<item name="materialAlertDialogTheme">@style/ThemeOverlay.MyApp.Dialog</item>

<style name="ThemeOverlay.MyApp.Dialog" parent="@style/ThemeOverlay.MaterialComponents.Dialog">
    <item name="android:dialogCornerRadius" tools:targetApi="p">@dimen/dp_4</item>
    <item name="android:paddingBottom">@dimen/dp_2</item>
    ...
</style>
Run Code Online (Sandbox Code Playgroud)