Button上显示图片

刚去中软外包公司面试,第一道题:

就是,怎样在按钮上显示图片?

 

<Button

  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:drawableTop="@drawable/icon"
  android:text="按钮"

/>

 

PS:就是这么的简单,我往复杂里想了,居然没有想到?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现在 button显示图片,可以使用 button 的 background 属性。可以使用以下步骤实现: 1. 在 res/drawable 文件夹下添加需要显示图片,例如将图片命名为 my_image.png。 2. 在布局文件中使用 button,并设置其 background 属性为图片的资源文件: ```xml <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/my_image" android:text="My Button" /> ``` 在这里,@drawable/my_image 将引用刚刚添加到 drawable 文件夹下的 my_image.png 文件。 3. 运行应用程序,你将看到在按钮上显示图片。 注意,如果你想要在按钮上显示一个点击效果的图片,你应该使用 buttonandroid:background 属性设置一个 selector。例如,你可以创建一个名为 my_button_selector.xml 的 selector 文件,类似于以下内容: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/my_image_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/my_image_normal"/> </selector> ``` 然后,在布局文件中将 button 的 background 属性设置为该 selector: ```xml <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/my_button_selector" android:text="My Button" /> ``` 这样,在按钮上将会显示 my_image_pressed.png 图片,当按钮被点击时,而 my_image_normal.png 图片将在按钮未被点击时显示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值