我希望获得类似于Material Components示例中的按钮形状
我已经尝试做的就是为这样的按钮设置自定义样式
<style name="ButtonAddLeft" parent="Widget.MaterialComponents.Button.Icon">
<item name="backgroundTint">@color/secondary</item>
<item name="android:textColor">@color/primary</item>
<item name="shapeAppearance">@style/ButtonAddLeftShape</item>
</style>
<style name="ButtonAddLeftShape">
<item name="cornerFamilyTopLeft">cut</item>
<item name="cornerFamilyBottomLeft">cut</item>
<item name="cornerSize">12dp</item>
</style>
但是,无论我如何设置cornerSize,这看起来都不像示例中的那个。
答案 0 :(得分:1)
您需要为主题设置切角样式。
<style name="RightCutButton" parent="ThemeOverlay.MaterialComponents.Light">
<item name="shapeAppearanceSmallComponent">@style/CornerCut</item>
</style>
<style name="CornerCut" parent="ShapeAppearance.MaterialComponents.SmallComponent">
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSizeTopRight">18dp</item>
<item name="cornerSizeBottomRight">18dp</item>
</style>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="shapeAppearanceSmallComponent">@style/CornerCut</item>
</style>
答案 1 :(得分:0)
您可以在布局中使用 app:shapeAppearanceOverlay
属性:
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="@style/ButtomShapeArrowRight"
.../>
具有:
<style name="ButtomShapeArrowRight">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">4dp</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSizeTopRight">50%</item>
<item name="cornerSizeBottomRight">50%</item>
</style>
您也可以通过编程实现它:
MaterialButton button_arrow = findViewById(R.id.button_arrow);
button_arrow.setShapeAppearanceModel(
button_arrow.getShapeAppearanceModel()
.toBuilder()
.setTopLeftCorner(CornerFamily.ROUNDED,..)
.setBottomLeftCorner(CornerFamily.ROUNDED,...)
.setBottomRightCorner(CornerFamily.CUT, new RelativeCornerSize(0.5f))
.setTopRightCorner(CornerFamily.CUT, new RelativeCornerSize(0.5f))
.build()
);
只是关于new RelativeCornerSize(0.5f)
的注释:它在1.2.0-beta01
中进行了更改。在new RelativeCornerSize(50))
之前。