简体   繁体   English

在Android中的按钮上设置背景图片

[英]Setting a background image on a Button in Android

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: 我的布局xml文件中包含以下内容:

<Button
    android:id="@+id/p1b1"
    android:layout_width="90px"
    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. 我的drawable-hdpi,drawable-ldpi和drawable-mdpi文件夹中有gridt.png图像。

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. 我想使用按钮而不是ImageButton,因为我也有按钮的文本。

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 取决于要相对于图片放置文本的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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