按钮中的渐变色背景

Gradient color background in button

我创建了一个 XML 文件,用于为我的按钮提供圆形和渐变颜色。

在布局文件中,尽管使用了 android:background="@drawable/button_color,但出现了白色按钮。

我需要用我的渐变色替换它。

有人可以查看我的代码吗?

提前致谢。

这是我的代码:

这是我的XML:res>drawable>button_color.xml

<shape
    android:shape = "rectangle"
    xmlns:android = "http://schemas.android.com/apk/res/android" >

    <corners android:radius="25dp" />
    <gradient android:angle="270"
        android:endColor="#36aec6"
        android:startColor="#8cc542" />

</shape>

布局文件:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp">

            <ImageView
                android:id="@+id/logoImage"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_marginStart="0dp"
                android:src="@drawable/logo"
                android:contentDescription="@string/acuvisor" />

            <ImageView
                android:id="@+id/companyName"
                android:layout_width="170dp"
                android:layout_height="match_parent"
                android:layout_toEndOf="@id/logoImage"
                android:src="@drawable/company_name"
                android:contentDescription="@string/acuvisor" />

            <TextView
                android:id="@+id/Help"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/help"
                android:layout_alignParentEnd="true"
                android:layout_centerInParent="true"
                android:textSize="16sp"
                android:clickable="true"
                android:padding="12dp"
                tools:ignore="RelativeOverlap" />


        </RelativeLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/languageRV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:layout_margin="12dp"/>

    <Button
        android:id="@+id/nextButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="@string/next"
        android:textColor="@color/black"
        android:background="@drawable/button_color"/>

</LinearLayout>

输出:

屏幕

使用 AppCompat 主题而不是像

这样的Material主题

Theme.AppCompat.DayNight.DarkActionBar

但是,如果您仍想使用 material 按钮,只需在设置渐变背景的行之前添加一个 app:backgroundTint="@null" 属性即可。