android - 为带轮廓的 Material Components 按钮的背景着色

标签 android android-layout android-button material-components-android material-components

我正在尝试创建一个 outlined Material Components button ,但是除了描边之外,我还需要它有一个半透明的背景。

到目前为止,这是我的 XML 代码:

<android.support.design.button.MaterialButton
    android:id="@+id/foo"
    style="@style/Widget.MaterialComponents.Button.TextButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:backgroundTint="#CFFF"
    app:strokeColor="@color/colorAccent"
    app:strokeWidth="2dp" />

这就是它的样子:

Button with 2dp stroke

问题是一些背景在按钮周围的笔划之外可见(笔划宽度越大,出现的白色像素越多)。

例如,这是一个 5dp 笔画:

Button with 5dp stroke

有没有办法解决这个问题,更好的设置背景颜色的方法,或者其他什么?

最佳答案

只需使用 Widget.MaterialComponents.Button.OutlinedButton 样式:

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        app:strokeWidth="2dp"
        app:strokeColor="@color/colorAccent"
        app:backgroundTint="#3ad64f"
        .../>

enter image description here

关于android - 为带轮廓的 Material Components 按钮的背景着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50571221/

相关文章:

android:导出的 Activity 不需要权限

android - Android API 21 上的 Material 切换按钮问题

android - 如果我需要按钮的小边框半径,为什么我们需要创建形状?

java - 使用 include 标签时键盘下一个按钮

java - 我可以根据变量而不是在 xml 中添加运行时按钮数量吗

java - ArrayList的值在匿名内部类之外是不同的

java - 如何从 FireBase 下载特定项目,但不是所有项目列表?

java - 设置选中复选框的限制

java - 与 MVVM 的套接字连接

Android Material Date Range Picker - 如何仅更改选定范围日期的文本颜色?