Android 按钮背景采用原色

Fat*_*aif 11 xml android button android-button material-components-android

我有这个问题,我不知道它来自哪里,我已经用自定义背景创建了这个按钮,但是背景颜色是原色,除非更改原色,否则无法更改它。

<Button
    android:id="@+id/btn_teacher"
    style="@style/normalText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/_16sdp"
    android:background="@drawable/btn_rounded_stroke"
    android:text="@string/txt_teacher" />

    <resources>
        <color name="colorPrimary">#008577</color>
        <color name="colorPrimaryDark">#00574B</color>
        <color name="colorAccent">#D81B60</color>
        <color name="bg_color">#FFD7A4</color>        
    </resources>
Run Code Online (Sandbox Code Playgroud)

我有很多不同颜色的按钮,所以我不能改变原色

这是我的可绘制背景

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >
        <shape android:shape="rectangle"  >
            <size android:height="@dimen/_48sdp" />
            <corners android:radius="@dimen/_24sdp" />
            <stroke android:width="@dimen/_1sdp" android:color="#59CECE" />
            <solid android:color="@android:color/white"/>
        </shape>
    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

我正在使用谷歌的新材料设计

implementation "com.google.android.material:material:1.3.0-alpha02"
Run Code Online (Sandbox Code Playgroud)

我怎样才能覆盖这种颜色? 这是按钮的图像

Gab*_*tti 20

由于您使用的是aTheme.MaterialComponents.*在运行时被 aButton替换。MaterialButton

目前backgroundTint仍然是默认 MaterialButton样式。这意味着如果您使用的是 custom android:background,则必须确保将其设为 nullbackgroundTint以避免自定义背景不会被attr/colorPrimary主题中定义的颜色着色。

你必须添加app:backgroundTint="@null"

  <Button
    app:backgroundTint="@null"
    android:background="@drawable/.."
Run Code Online (Sandbox Code Playgroud)

在任何情况下,您都不需要自定义背景 ( btn_rounded_stroke)。您只是使用自定义背景来定义圆角。默认情况下,此功能由 提供MaterialButton,然后只需使用该cornerRadius属性。

使用标准MaterialButton

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        app:strokeColor="#59CECE"
        app:cornerRadius="24dp"
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


小智 5

当使用 Material 主题时,按钮视图将映射到 MaterialButtton。在这种情况下,还有另一种方法来设置按钮背景。

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar"
...
        <item name="materialButtonStyle">@style/ColoredButton</item>
    </style>

    <style name="ColoredButton" parent="Widget.MaterialComponents.Button.TextButton">
        <item name="backgroundTint">@color/customButtonColor</item>
    </style>
Run Code Online (Sandbox Code Playgroud)


Pra*_*iya -1

尝试这个 。

您的按钮:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:layout_margin="5dp"
    android:background="@drawable/round_btn"
    android:padding="20dp"
    android:text="My Text" />
Run Code Online (Sandbox Code Playgroud)

这是您的round_btn可绘制对象:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#008577" />
    <corners android:radius="20dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)