【问题标题】:How can I change the corner radius of Material Components Dialog (M3) in Android如何在 Android 中更改 Material Components Dialog (M3) 的角半径
【发布时间】:2022-01-17 14:56:24
【问题描述】:

查看图片here

我第一次在我的项目中使用 MDC(3),在我看到对话框之前一切都很顺利。这有什么问题?

根据文档,圆角半径的大小为 28dp

如何减小拐角半径的大小?

更新这里是我的代码(我刚刚根据@che10的回答更新了

themes.xml

<style name="AppTheme" parent="Theme.Material3.Light.NoActionBar">
</style>

styles.xml

<style name="MaterialAlertDialog.App" parent="MaterialAlertDialog.Material3">
        <item name="shapeAppearance">@style/ShapeAppearance.App.MediumComponent</item>
    </style>
    <style name="ShapeAppearance.App.MediumComponent" parent="ShapeAppearance.Material3.MediumComponent">
        <item name="cornerSize">10dp</item>
    </style>

在 Java 中

new MaterialAlertDialogBuilder(this,R.style.MaterialAlertDialog_App)

它仍然显示与默认相同的圆角半径 (28dp)

【问题讨论】:

    标签: android material-components-android


    【解决方案1】:

    在您的 styles.xml 中创建并添加以下内容

    <style name="MaterialAlertDialog.App" parent="MaterialAlertDialog.Material3">
        <item name="shapeAppearance">@style/ShapeAppearance.App.MediumComponent</item>
    </style>
    
    <style name="ShapeAppearance.App.MediumComponent" parent="ShapeAppearance.Material3.MediumComponent">
        <item name="cornerSize">20dp</item>
    </style>
    

    在实例化您的 MaterialAlertDialogBu​​ilder 时,将其直接传递到那里,您应该可以开始了。像这样的

    MaterialAlertDialogBuilder(requireContext(), R.style.MaterialAlertDialog.App)
    

    您可以在此处找到更多信息: https://m3.material.io/components/dialogs/implementation/android

    我觉得你还需要修改themes.xml

    <style name="AppTheme" parent="Theme.Material3.Light.NoActionBar">
      <item name="materialAlertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog</item>
    </style>
    
    <style name="ThemeOverlay.App.MaterialAlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
        <item name="alertDialogStyle">@style/MaterialAlertDialog.App</item>
    </style>
    

    【讨论】:

    • 如果你错过了,我使用的是 MDC 版本 1.5.0。他们已经用 ThemeOverlay.Material3 替换了 ThemeOverlay.MaterialComponents
    • @RocketDev 我的错。我错过了。过程仍然保持不变,只是父主题会得到更新。让我更新一下。
    • 感谢您的帮助,但它并没有像我想要的那样工作。我刚刚更新了这篇文章,可能对你回答我的问题有帮助。谢谢老兄。
    • @RocketDev 漏掉了一些步骤,还需要修改themes.xml,看材料文档,可以修改圆角半径。
    猜你喜欢
    • 1970-01-01
    • 2014-06-12
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多