gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:32 26 4
gpt4 key购买 nike

我想知道如何将图像放入按钮。

我试过使用 "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>

最佳答案

 <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 布局中使用属性,将属性定义为放置在按钮内的图像源

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

关于android - 如何将图像添加到android中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18780462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com