如何将图像添加到android中的按钮

Anm*_*hwa 8 android android-layout

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

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

我到目前为止编写的代码.

<?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>
Run Code Online (Sandbox Code Playgroud)

Vik*_*tel 35

 <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"/>
Run Code Online (Sandbox Code Playgroud)

添加backgroundimage同时添加text.

只需更换

android:background="@drawable/addgroup"
Run Code Online (Sandbox Code Playgroud)

android:background="@android:color/transparent"
android:drawableTop="@drawable/addgroup"
android:text="TEXT"
Run Code Online (Sandbox Code Playgroud)

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

android:drawableLeft
android:drawableRight
android:drawableBottom
android:drawableTop
Run Code Online (Sandbox Code Playgroud)

  • 实际上在名称中不包含".png",它会给出错误,说你的资源无法找到.只需使用`android:background ="@ drawable/addgroup"` (2认同)