着色概述的材料成分按钮的背景

时间:2018-05-28 17:36:37

标签: android android-layout material-components material-components-android

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

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

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:layout_centerHorizontal="true"      
    android:layout_centerVertical="true"      
    android:max="100" 
    />

这就是这个:

Button with 2dp stroke

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

例如,这是一个<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 5dp stroke

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

3 个答案:

答案 0 :(得分:1)

只需使用 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

答案 1 :(得分:0)

你可以尝试在xlm中直接在drawables中创建你的按钮:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="backgroundcolor"/>
        <stroke android:color="strokecolor" android:width="2dp" />
        <!--corners allow us to make the rounded corners button-->
        <corners android:radius="15dp" />
    </shape>
</item>
</selector>

在此创建:https://android--code.blogspot.fr/2015/01/android-rounded-corners-button.html

并在你的布局中使用它:

 android:background="@drawable/nameofbutton.xml"

答案 2 :(得分:0)

如果您创建一种样式,则必须删除app:,在我的示例中,该样式称为SecondButton

 <style name="SecondButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
    <item name="android:theme">@style/SecondButtonTheme</item>
    <item name="backgroundTint">@color/second_button_back</item>
    <item name="strokeColor">@color/second_button_text</item>
    <item name="strokeWidth">1dp</item>
    <item name="android:textColor">@color/second_button_text</item>
</style>

像这样在自己中使用它

<com.google.android.material.button.MaterialButton
        style="@style/SecondButton"
    .../>