如何在Android中为按钮添加图像?

43

如何在按钮上添加图像而不是文本?

10个回答

103

考虑到您的标签,幽默地说,只需使用ImageButton小部件即可。


27

只需使用ImageButton视图并为其设置图像:

 <ImageButton
    android:id="@+id/searchImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:src="@android:drawable/ic_menu_search" />

16

正如他所述,使用ImageButton小部件。将您的图像文件复制到项目的Res/drawable/目录中。在XML中,只需进入您的XML文件的图形表示(为简单起见),然后单击添加的ImageButton小部件,转到其属性表并单击src:字段中的 [...] 。只需导航到您的图像文件。此外,请确保您正在使用正确的格式;出于我的原因,我倾向于使用.png文件,但它们有效。


非常有用的回复,谢谢!我不知道在哪里放置图片或如何指定它们的位置。 - hubatish

8
你应该尝试这样做:
    <Button
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/qrcode"/>

android:background="@drawable/qrcode"将会实现它。


1
当使用wrap_content时,ImageButton总是选择正确的大小。而Button可能会扭曲图像,特别是当它们很小的时候。 - Chilloutman

7
新的 Material Components 中的 MaterialButton 可以包含图标:
<com.google.android.material.button.MaterialButton
    android:id="@+id/material_icon_button"
    style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/icon_button_label_enabled"
    app:icon="@drawable/icon_24px"/>

您还可以自定义一些图标属性,例如iconSizeiconGravity

参考此处


6
   <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="News Feed"
        android:icon="@drawable/newsfeed" />

新闻源是存在drawable文件夹中的图片。


12
无法正常运作。 要使带有文本和图标的按钮出现,请使用Button类和android:drawableLeft属性:
抱歉回复晚了,但它对我有效。 您没有为按钮分配文本,因此它不会出现。 问题是显示带有图像的按钮而不是文本 :) - Amr Angry

3
将您的图片放入drawable文件夹中。这里我图片的文件名为left.png。
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="118dp"
    android:layout_y="95dp"
    android:background="@drawable/left"
    android:onClick="toast"
    android:text=" " />

3

在xml文件中使用android:drawableright或android:drawabletop属性来设置按钮的图标。

例如:在xml文件中为按钮添加以下属性:

Android:drawableleft="@drawable/ic_person"

或者

Android:drawableright="@mipmap/ic_launcher"


2
您可以在Android的activity_main.xml中创建一个ImageButton,将您想要放置在按钮上的图片粘贴到drawable文件夹中,以下是代码示例供您参考。
<ImageButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="49dp"
    android:layout_weight="1"
    android:onClick="prev"
    android:src="@drawable/prev"
    />

0

你可以将ImageView用作按钮。创建一个ImageView,并在之后将clickable设置为true,然后编写imageView.setOnClickListener来为ImageView设置点击事件。

  <ImageView
android:clickable="true"
android:focusable="true"`
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

并在Activity的onCreate方法中:

imageView.setOnClickListener(...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接