我希望我的按钮具有圆角,例如:
[在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"/>
代替。
对于其他所有按钮类型,我都必须这样做。我想知道这种方法是否正确,如果不正确,有人可以指导我如何正确执行此操作吗?
非常感谢。
您可以在按钮上设置可绘制背景。 drawable可能看起来像这样:
<shape android:shape="rectangle">
<solid android:color="@android:color/blue" />
<corners android:radius="10dp" />
</shape>
这将为您的按钮背景提供圆角。