如何在Android中将图像添加到按钮 [英] How to add an image to a button in android

查看:164
本文介绍了如何在Android中将图像添加到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将图像放置到按钮上.

I was wondering how I put an image to a button.

我尝试使用"android:icon ="@ drawable/search.png" 并且我将此图像添加到drawable-hdpi文件夹中,但该图像没有显示.我正在开发nexus 4的应用程序,所以我不知道图像应该是多大.我要添加的图像是在nexus 4的联系人管理器中使用的常规搜索图标.

I have tried to use "android:icon="@drawable/search.png" and I add this image to the drawable-hdpi folder but the image doesn't show up. I am developing an application for the nexus 4 so I don't know what size the image should be. The image I am trying to add is a normal search icon used in the contact manager of the nexus 4.

到目前为止我写的代码.

The code I have written so far.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lbl_group_coworkers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Coworkers"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

    <TextView 
        android:id="@+id/lbl_group_family"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Family"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>
    <TextView
        android:id="@+id/lbl_group_friends"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Friends"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="vertical" >


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/Button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.70"
            android:icon="@drawable/search.png" />

        <Button
            android:id="@+id/Button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="addGroup"
            android:icon="@drawable/addgroup.png"/>

        <Button
            android:id="@+id/Button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.11"
            android:text="@string/Button3" />
    </LinearLayout>

</LinearLayout>

推荐答案

 <Button
        android:id="@+id/Button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="addGroup"
        android:text="TEXT"
        android:background="@drawable/addgroup"/>

要同时添加backgroundimagetext.

只需更换

android:background="@drawable/addgroup"

使用

android:background="@android:color/transparent"
android:drawableTop="@drawable/addgroup"
android:text="TEXT"

您还可以将 Button 布局中的属性与定义要放置在按钮内的图像源的属性一起使用

You can also use attributes in your Button Layout with properties defining image source to placed inside the button

android:drawableLeft
android:drawableRight
android:drawableBottom
android:drawableTop

这篇关于如何在Android中将图像添加到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆