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

4 回答 4

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

同时添加background和。imagetext

只需更换

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
于 2013-09-13T07:26:32.920 回答
0

使用属性

android:background="@drawable/...."
于 2013-09-13T07:25:12.353 回答
0

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

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

于 2013-09-13T07:26:28.003 回答
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"
                 />
于 2013-09-13T08:59:32.947 回答