How to use MaterialAlertDialogBuilder fine?

你。 提交于 2020-08-27 09:12:07

问题


When i use dialog.builder the font size is correct but when i use MaterialAlertDialogBuilder the font size of body text is smaller. its ok?

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

Im use this theme

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

MaterialComponent code

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

Screenshot_20190512-115103_2

AlertDialog.Builder

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

Screenshot_20190512-115241_2

Where is the problem?


回答1:


It is intentional. They are using different styles.

You can change it using something like:

  <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>

and then:

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




回答2:


You can solve like this:

<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>



回答3:


You need to use MaterialAlertDialogBuilder instead of AlertDialog.Builder.

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


来源:https://stackoverflow.com/questions/56098162/how-to-use-materialalertdialogbuilder-fine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!