首页 > 解决方案 > xml中的渐变不会出现在按钮上

问题描述

我在我的可绘制资源文件 (.xml) 中创建了一个渐变,然后将其连接到按钮的(在 activity_main.xml 中)背景。不幸的是,这个文件中的所有结构都没有出现在这个按钮上。我是初学者。这是代码问题还是其他问题?渐变可以用作 ImageView 甚至可以用作 Switch,但不能用作按钮的背景。gradient.xml 中的代码:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <item>
        <shape>
              <gradient
                   android:angle="90"
                   android:endColor="#b5b6d2"
                   android:startColor="#555994"
                   android:type="linear" />
               <corners
                   android:radius="0dp"/>
               <padding
                   android:right="10dp"
                   android:top="10dp"
                   android:bottom="10dp"
                   android:left="10dp"/>
        </shape>
    </item>
</selector>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:background="@drawable/gradient"
        android:text="@string/button"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidxmlandroid-studioandroid-layout

解决方案


当您想根据小部件的不同状态显示不同的背景/颜色时,您可以使用选择器。对于您的情况,用您的可绘制文件中的代码替换您的代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
<gradient
    android:angle="90"
    android:endColor="#b5b6d2"
    android:startColor="#555994"
    android:type="linear" />
<corners
    android:radius="0dp"/>

</shape>

更正: Button 小部件将 colorAccent 作为默认背景奇怪地将小部件更改为 AppCompatButton ,它将解决问题。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="Button"
        android:background="@drawable/gradient"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读