android - 如何实现这个 Material Design 2.0 按钮形状?

标签 android material-design android-button material-components-android material-components

我想实现看起来像 Material Components 示例中的按钮形状

example

我已经尝试做的是像这样为按钮设置自定义样式

    <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。

最佳答案

需要根据主题设置切角样式。

    <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>

关于android - 如何实现这个 Material Design 2.0 按钮形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55611915/

相关文章:

android - 如何自定义android Material 调色板

Android Material 设计 NestedScrollView/CollapsingToolbarLayout 打开键盘的奇怪行为

android - 如何访问 onClick 事件处理程序中的 AttributeSet 以便在 obtainStyledAttributes 中使用?

Android:如何使按钮导致 ListView 项目详细信息(在不同的 Activity 中)(主/详细流程)

启动时选择的 Android Spinner 设置项

android - Firebase 和 Fabric 集成 : No detectable Fabric Event in Firebase

android - 单击按钮时 TextView 未更新

android - 如何禁用 Android Lollipop 波纹的 alpha 值?

java - while 无法循环

java - 如何在 Activity 类中启用 xml Button?