在按钮背景中添加图像时出现问题

Problem while adding an image in the background of a button

我正在制作一个音乐播放器应用程序。当我尝试在按钮的背景中添加图像时,出现白色背景并且没有显示图像。

帮我找出错误。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="128dp"
        android:layout_height="62dp"
        android:background="@drawable/icons8-material-rounded-icons-96" />
</RelativeLayout>

当您将图像复制到可绘制对象时,您被要求在两个文件夹之间移动移动到另一个非 v24 的文件夹。

步骤:

  • 删除图片
  • 再次将图像复制到drawable文件夹,这次选择非v24的其他文件夹 这将解决您的问题

有关详细信息,请查看此答案

如果您想显示图片而不是文本,请尝试使用 Image button

<ImageButton
    android:src="@drawable/icons8-material-rounded-icons-96"
    android:id="@+id/button2"
    android:layout_width="128dp"
    android:layout_height="62dp" />

还要确保您已按照@GOVIND DIXIT 所述将图像移动到可绘制文件夹

图片名称不正确,在构建这个项目时出现错误,请更改图片名称icons_material_rounded_icons_96,然后它会显示[=背景14=] 对我有用。

你可以使用 ImageView

 <ImageView
                android:id="@+id/dateClosure"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"
                android:background="@color/secondary_color"  
                android:layout_weight="1.5"
                android:src="@drawable/calender2" />   

背景:- 这是显示图像的背景。

src:- 此处放置您的图片,它将显示在您的背景上方