开发者

Setting a background image on a Button in Android

开发者 https://www.devze.com 2023-04-11 01:59 出处:网络
I am having a problem trying to set the background of a button to be a bitmap image. I have the following in my layout xml file:

I am having a problem trying to set the background of a button to be a bitmap image. I have the following in my layout xml file:

<Button
    android:id="@+id/p1b1"
    android:layout_width="开发者_如何学Python90px"
    android:layout_height="60px"
    android:layout_alignParentLeft="true"
    android:background="@drawable/gridt"


    android:text="" />

This is just the part for the button. I have the gridt.png image in my drawable-hdpi, drawable-ldpi and drawable-mdpi folders.

The button image simply shows a white box on the empty button and the corners are not rounded.

I need to do this on several buttons, but learning how to efficiently do this on one button will be a good starting point. I would like to use a Button and not an ImageButton as I have text o the button as well.

Thanks.


Use

android:drawableTop="@drawable/gridt"

This will put the text below your drawable on the button

Alternatively you can use

android:drawableBottom="@drawable/gridt"
android:drawableLeft="@drawable/gridt"
android:drawableRight="@drawable/gridt"

depending on where you want the text placed relative to the picture

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号