材质按钮上的圆角

新手上路,请多包涵

我正在按照 此类 问题的提示创建一个按钮样式,如 Material Design 上建议的那样。

按钮

但是,我需要更改圆角半径,但无法通过继承 Widget.AppCompat.Button.Colored 样式并设置半径参数来做到这一点。

我怎样才能拥有相同的风格但圆角?

原文由 juliano.net 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 478
2 个回答

更新:

下面Gabriele Mariotti的回答现在更好了。

老答案:

你需要继承这种风格。

添加到你的styles.xml:

  <style name="AppTheme.RoundedCornerMaterialButton" parent="Widget.AppCompat.Button.Colored">
        <item name="android:background">@drawable/rounded_shape</item>
 </style>

添加文件drawable/rounded_shape.xml:

 <shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"   >

    <solid
        android:color="@color/colorAccent" >
    </solid>

    <corners
        android:radius="11dp"   >
    </corners>

</shape>

最后在您的布局中:

  <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Test Text"
       style="@style/AppTheme.RoundedCornerMaterialButton"/>

编辑:更新了使用主题颜色而不是硬编码颜色的答案。

原文由 Nikita Shaposhnik 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果您不想更改整个应用程序的主题。您可以专门为此视图使用材料主题:

 <com.google.android.material.button.MaterialButton
    android:id="@+id/fooButon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:fontFamily="sans-serif"
    android:padding="8dp"
==> android:theme="@style/Theme.MaterialComponents.Light"
    app:backgroundTint="@color/base_white" />

原文由 Mohit Yadav 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题