如何使按钮以材料设计主题为圆角?

问题描述 投票:0回答:1

我希望我的按钮具有圆角,例如:

Rounded Button //图像取自Google

[在Android中使用材质主题实现此目的是将shapeAppearanceSmallComponent设置为具有rounded角。

但是设置shapeAppearanceSmallComponent也会影响所有其他成分,例如EditText,因此现在也将其舍入。

因此,我没有将其设置为shapeAppearanceSmallComponent,而是创建了shapeMaterialOverlay。将此叠加层设置为buttonStyle,并将主题中的此按钮样式设置为默认按钮样式。

它有效,但仅适用于默认按钮。如果需要这样的TextButton

<Button
    ...
    style="@style/Widget.MaterialComponents.Button.TextButton"/>

TextButton将不会被唤醒。因此,作为一种解决方法,我创建了从MyTextButton扩展的TextButton样式,并在那里也设置了shapeOverlay

所以现在,如果我需要TextButton,我会做:

<Button
    ...
    style="@style/Widget.MaterialComponents.Button.MyTextButton"/>

代替。

对于其他所有按钮类型,我都必须这样做。我想知道这种方法是否正确,如果不正确,有人可以指导我如何正确执行此操作吗?

非常感谢。

android material-design android-button
1个回答
0
投票

您可以在按钮上设置可绘制背景。 drawable可能看起来像这样:

<shape android:shape="rectangle">
    <solid android:color="@android:color/blue" />
    <corners android:radius="10dp" />
</shape>

这将为您的按钮背景提供圆角。

© www.soinside.com 2019 - 2024. All rights reserved.