如何在按钮上添加图像(左)和文本

Posted

技术标签:

【中文标题】如何在按钮上添加图像(左)和文本【英文标题】:How to add image(left) and text on button 【发布时间】:2013-06-23 06:53:10 【问题描述】:

如何在按钮上添加图片(左)和文字?


为了说明:

【问题讨论】:

【参考方案1】:

在您的布局 xml 中使用 android:drawableLeft="@drawable/image"

你也可以从代码中做到这一点

Drawable icon= getContext().getResources().getDrawable( R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );

【讨论】:

非常好。是否可以更改可绘制的背景颜色?因为我的图标是透明背景的白色。 它可能会帮助你***.com/questions/4692642/… 别担心,我还在尝试你的答案。是否可以用选择器设置drawableLeft? 是的,我知道了 :) 朋友,感谢您的输入:drawableLeft 和 Selector 指南! :) 它就像魅力一样。 "android:drawableLeft.." drawable中的d必须小写【参考方案2】:
        android:text="Documentos"
        android:drawableLeft= "@drawable/ic_document"

【讨论】:

【参考方案3】:

我们还可以在编程的同时管理drawable的大小

Drawable dr = getResources().getDrawable(R.drawable.mag_green);
dr.setBounds(0, 0, 43, 40); //Left,Top,Right,Bottom
search_bar.setCompoundDrawables(dr, null , null , null);

【讨论】:

以上是关于如何在按钮上添加图像(左)和文本的主要内容,如果未能解决你的问题,请参考以下文章