2010-11-17 101 views

回答

9

正如他所说,使用了ImageButton小部件。将您的图像文件复制到项目的Res/drawable/目录中。在XML中,只需进入XML文件的图形表示(为了简单起见),然后单击您添加的ImageButton小部件,然后转至其属性页并单击src:字段中的[...]。只需导航到您的图像文件。另外,请确保您使用的是正确的格式;由于我自己的原因,我倾向于坚持使用.png文件,但它们工作正常。

+0

实际上有帮助的回复,谢谢!我不知道在哪里放置图像或如何指定它们的位置。 – hubatish 2016-02-16 18:56:15

6
<Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="News Feed" 
     android:icon="@drawable/newsfeed" /> 

新闻源则在绘制文件夹

+9

这不起作用。 为了使按钮出现的文本和图标,则使用按钮类与机器人:drawableLeft属性: '<按钮 机器人:layout_width =“WRAP_CONTENT” 机器人:layout_height =“WRAP_CONTENT” 机器人:text =“@ string/button_text” android:drawableLeft =“@ drawable/button_icon”>' – 2013-02-24 17:25:09

+0

对不起,我的后期重播,但它适用于我 你不分配文本的按钮,所以它不会出现 和问题是显示按钮与图像,而不是文本:) – 2014-10-14 09:50:31

7

形象你应该尝试这样

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

android:background="@drawable/qrcode"东西会做

+0

[ImageButton](https://developer.android.com/refe rence/android/widget/ImageButton.html)在使用'wrap_content'时总是选择正确的大小。 “按钮”可以扭曲图像,特别是当它们很小时。 – Chilloutman 2016-07-29 16:19:00

3

把你的形象绘制文件夹中。在这里我的图像文件名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=" " /> 
12

只需使用ImageButton的查看和设置图片吧:`

<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

你可以在你的Android activity_main.xml中创建的ImageButton和图像你想放置在您的按钮只是将该图像粘贴到您的可绘制文件夹下面是示例代码供您参考。

<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" 
    />