如何在安卓中给按钮添加图片

9

我想知道如何将图片添加到按钮上。

我尝试使用“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>

4
使用 ImageButton 替代 button - Raghunandan
5个回答

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

同时添加背景图片文本

只需替换

android:background="@drawable/addgroup"

随着

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

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

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

我做了那个,但是图片消失了。有什么想法吗? - Anmol Wadhwa
尝试将android:backgroundandroid:text一起添加到Button属性中 :) @AnmolWadhwa - Vikalp Patel
2
实际上不要在名称中包含“.png”,否则会出现错误,提示找不到您的资源。只需使用android:background="@drawable/addgroup"即可。 - Azurespot

0
使用属性。
android:background="@drawable/...."

0
请在res目录下创建drawable文件夹,并在drawable文件夹中创建buttonanimation.xml文件。
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/add_incident_resetfocus" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/add_incident_resetfocus" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/add_incident_resetfocus" android:state_focused="true"/>
<item android:drawable="@drawable/add_incident_reset" android:state_focused="false" android:state_pressed="false"/>
</selector>

并将此按钮动画用于布局按钮背景。

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

我按照上述步骤操作了,但是没有产生任何效果。 - cammando

0

您可以使用以下方式设置按钮的背景:

android:background="@drawable/..."


0

只需将此代码放在按钮内部:

// 仅适用于 API >21,如果您想要给图像添加阴影

android:background="@drawable/your_drawable_or_png_file" app:backgroundTintMode="add"

// 否则

android:background="@drawable/your_drawable_or_png_file"

对我有效!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接