How to add image for button in android?

前端 未结 9 1038
再見小時候
再見小時候 2021-02-02 06:44

How can I add an image to a button, rather than text?

相关标签:
9条回答
  • 2021-02-02 07:19

    Rather humorously, considering your tags, just use the ImageButton widget.

    0 讨论(0)
  • 2021-02-02 07:21

    Simply use ImageButton View and set image for it:`

     <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" />
    
    0 讨论(0)
  • 2021-02-02 07:23

    You can create an ImageButton in your android activity_main.xml and which image you want to place in your button just paste that image in your drawable folder below is the sample code for your reference.

    <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 讨论(0)
  • 2021-02-02 07:30

    You should try something like this

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

    the android:background="@drawable/qrcode" will do it

    0 讨论(0)
  • 2021-02-02 07:30

    Put your Image in drawable folder. Here my image file name is 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=" " />
    
    0 讨论(0)
  • 2021-02-02 07:31

    As he stated, used the ImageButton widget. Copy your image file within the Res/drawable/ directory of your project. While in XML simply go into the graphic representation (for simplicity) of your XML file and click on your ImageButton widget that you added, go to its properties sheet and click on the [...] in the src: field. Simply navigate to your image file. Also, make sure you're using a proper format; I tend to stick with .png files for my own reasons, but they work.

    0 讨论(0)
提交回复
热议问题