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

las*_*zlo 5 android android-button material-design material-components material-components-android

我想实现看起来像材料组件示例中的按钮形状

例子

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

    <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>
Run Code Online (Sandbox Code Playgroud)

但这看起来不像示例中的那个,无论我如何设置cornerSize。

San*_*ali 4

您需要将切角样式设置为主题。

    <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>
Run Code Online (Sandbox Code Playgroud)

  • 创建一个单独的主题并将其设置为 xml 中的按钮。&lt;style name="RightCutButton"parent="ThemeOverlay.MaterialComponents.Light"&gt; &lt;item name="shapeAppearanceSmallComponent"&gt;@style/CornerCut&lt;/item&gt; &lt;/style&gt; 现在,在您已声明的 XML 中设置此 RightCutButton使用“android:theme=”@style/RightCutButton”按钮 这仅覆盖该特定按钮。@laszlo (3认同)