如何在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"/>
要同时添加background
和image
和text
.
只需更换
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