【问题标题】:app:cornerRadius is not working in android material design buttonapp:cornerRadius 在 android 材料设计按钮中不起作用
【发布时间】:2023-03-07 21:17:01
【问题描述】:

如何改变android材质按钮的圆角半径。我尝试设置角半径,但它不起作用。

<com.google.android.material.button.MaterialButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="@color/colorPrimary"
                android:text="Start"
                android:textColor="@android:color/white"
                android:textSize="18sp"
                app:cornerRadius="8dp" />

【问题讨论】:

标签: android android-button android-theme material-components-android material-components


【解决方案1】:

不要使用“背景”属性——这是被禁止的技术。请改用“backgroundTint”。

【讨论】:

    【解决方案2】:

    首先,这意味着您没有使用Material Components theme继承的主题。
    它是材质组件主题中的属性,它定义了MaterialButton 使用的默认样式:

    <item name="materialButtonStyle">@style/Widget.MaterialComponents.Button</item>
    

    仅在这种情况下您还必须通过style="@style/Widget.MaterialComponents.Button" 指定文本字段样式:

    <com.google.android.material.button.MaterialButton
       style="@style/Widget.MaterialComponents.Button"
       ..>
    

    最后说明:从1.1.0 开始,您必须使用 Material Components 主题或 Bridge 主题。

    【讨论】:

    • 这是正确答案!您应该首先将它们应用到您的应用中。
    【解决方案3】:

    您需要像下面这样设置 Button 的 style

    要使用 Material Design,您需要在 dependencies 下方添加 gradle

    dependencies { implementation ‘com.google.android.material:material:1.1.0’ }
    

    XML

    <com.google.android.material.button.MaterialButton
    
       style="@style/Widget.MaterialComponents.Button"
       app:cornerRadius="8dp"/>
    

    【讨论】:

    • 不正确。 @style/Widget.MaterialComponents.Button 是 Material Components 主题应用的默认样式。如果您需要添加它,可能是您在应用中使用了错误的主题。
    猜你喜欢
    • 2022-01-18
    • 2020-02-09
    • 2016-06-28
    • 2015-01-04
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    相关资源
    最近更新 更多