android学习,在按钮或者文字上添加图片

Drawable rightDrawable = getResources().getDrawable(R.drawable.reddot);
					rightDrawable.setBounds(0, 0,
							rightDrawable.getMinimumWidth(),
							rightDrawable.getMinimumHeight());

获取想要放置的图片

通过setBounds来设置图片的位置和宽高度

xxx.setCompoundDrawables(null, null,rightDrawable, null);
在xxx的左上右下中的右边来放置该图片

xxx.setCompoundDrawables(null, null,null, null);
取消xxxx上的该图片


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,您可以使用 ImageButton 或 Button 控件来创建一个同时包含图像和文本的按钮。 如果您想要创建一个 ImageButton,可以参考以下代码: ```xml <ImageButton android:id="@+id/myImageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/myIcon" android:contentDescription="@string/myIconDescription" /> ``` 在上面的代码中,我们使用了 android:src 属性来设置按钮上的图像,并使用 android:contentDescription 属性来为图像添加描述。您可以将 @drawable/myIcon 替换为您自己的图像资源,并将 @string/myIconDescription 替换为您自己的描述文本。 如果您想要创建一个 Button,可以参考以下代码: ```xml <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/myIcon" android:text="@string/myButtonText" /> ``` 在上面的代码中,我们使用了 android:drawableLeft 属性来设置按钮上的图像,并使用 android:text 属性来设置按钮上的文本。您可以将 @drawable/myIcon 替换为您自己的图像资源,并将 @string/myButtonText 替换为您自己的按钮文本。 请注意,您可以在任何类型的 Button 控件上使用 android:drawableLeft、android:drawableRight、android:drawableTop 或 android:drawableBottom 属性来添加图像。这些属性会将图像放置在文本的左侧、右侧、上方或下方。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值