简体   繁体   中英

unable to set next image inside button in android

I have a button inside my main_activity.xml layout. I have create a layout file for my button. I'm setting background of the button pointing to xml layout file: round_button.xml and android:src property to an image which is a next > sign. But the image is not displaying inside the button. How can I display an image inside my button. I would've prefer if my round_button.xml would've contain the image. Is this possible to do it with a vector file. What's the best way to do this?

<Button
    android:id="@+id/scan_button"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:background="@drawable/round_button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_next_button_img"
    android:layout_marginRight="10dp"/>

round button file

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <size android:height="60dp" android:width="60dp"/>
    <solid android:color="@color/colorPrimary"/>
    <corners android:radius="40dp"/>
</shape>

Use ImageView instead of Button . You can place there your image and vector background

<ImageView
android:id="@+id/scan_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/ic_next_button_img"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>

You can use android:drawable directionhere . For example

 android:drawableLeft="@drawable/IDhere"

Your image will be set on your button on the left. Text will continue from there.

<Button
android:id="@+id/scan_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/round_button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:drawableLeft="@drawable/ic_next_button_img"
android:layout_marginRight="10dp"/>

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