简体   繁体   中英

How to put image on button in android studio

I have a button with xml background. And I want to put image on this button. It is not possible to put two backgrounds in one button. So, is there other way of putting image on my button while I have had a background yet? Below the code of my button:

<Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="0"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_margin="15dp"
            android:background="@drawable/readmore_back"
             />  

you may use

<ImageButton
 ..
 ..
 ..
 android:src="@drawable/image1"
/>

instead of

   <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:layout_row="0"
    android:layout_gravity="fill"
    android:layout_rowWeight="1"
    android:layout_columnWeight="1"
    android:layout_margin="15dp"
    android:background="@drawable/readmore_back"
     />  

Or

if you want to set the image to your existing Button then use

  • android:drawableTop ,
  • android:drawableBottom ,
  • android:drawableLeft ,
  • android:drawableRight

Use ImageButton instead of Button

<ImageButton
    android:src="@drawable/my_image"
    android:text="@string/IMAGE"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

You can try this:

<ImageView
            android:id="@+id/battery"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:clickable="true"
            android:src="@drawable/laughing"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM