Android将图像添加到按钮

时间:2016-08-03 02:50:08

标签: java android image button

我正在尝试将图像添加到按钮(图像按钮或普通按钮),但不是在特定的res / drawable文件夹中。
我希望这个图像作为ImageView对象或可绘制对象等,但我不能。

2 个答案:

答案 0 :(得分:5)

您可以通过在按钮的XML中添加以下属性来设置Button的图标,如下所示:

android:drawableLeft="@drawable/button_icon"

如果您想知道如何以编程方式执行此操作,请按照以下文章操作: How to programmatically set drawableLeft on Android button?

或者只是添加'背景':

<Button
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/qrcode"/>

但我认为如果你使用ImageButton会更好,请看这个例子:

<ImageButton
    android:id="@+id/searchImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/your_image_drawable" />

答案 1 :(得分:-1)

使用ImageButton并在后台添加src

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/yourImageNameHere"
相关问题