如何在 EditText 上添加图像
我想在 EditText
中动态添加图像。是否可以?
如果有人知道请给出示例代码。
I want to dynamically add image in EditText
. Is it possible?
if anyone knows please give sample code for that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您正在谈论的是这样的内容:
,那么您只需设置
Drawable {对|左|顶部 | xml中的Bottom}
属性,或者调用相应的java命令。If something like this:
is what you're talking about, then you just need to either set the
Drawable{Right | Left | Top | Bottom}
property in the xml, or call the corresponding java command.使用框架布局很容易克服这个问题。这里的另一个优点是你可以给出点击事件对于按钮。如果您使用 setCompoundDrawables 设置图标,则无法提供单击事件。我已经在我的项目中实现了搜索栏具有删除和搜索图标。
Using the frame layout it is very easy to overcome this.Here another advantage is that you can give click events for buttons.if you set icons using setCompoundDrawables, you cant give the click events.I have implemented in my project that search bar has delete and search icons.
使用 android:drawableRight 或 android:drawableLeft
(取决于您喜欢在哪里对齐图像)
using
android:drawableRight
orandroid:drawableLeft
(depend where you prefer align image)
你也可以试试这个
you can also try this
您可以使用 setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int Bottom)。
You can use setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom).
您可以通过
android:background="@drawable/img"
将图像添加到EditText
中。如果您想使用九个补丁或其他方式修改样式,但如果您想在
EditText
左侧添加小图像,请考虑使用android:drawableRight="@drawable/icon “
。You can add an image to your
EditText
throughandroid:background="@drawable/img"
.If you want to modify the style by using nine patch or else, but if you want to add a small image in the left of your
EditText
consider usingandroid:drawableRight="@drawable/icon"
.您可以使用:
按照此处的建议:
调用 setCompoundDrawables() 不会显示复合 Drawable
You can use:
as suggested here:
Calling setCompoundDrawables() doesn't display the Compound Drawable
创建
EditText
的实例,然后创建图像的可绘制实例
,或者
然后通过调用
setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable Bottom); 来设置图像
上面的行将在
EditText
的右侧设置图像Create an instance of the
EditText
Then create a drawable instance of the image
OR
Then set the image by calling
setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);
The above line will set the image in the right side of the
EditText
如果您属于适配器类,则可以尝试此操作
并且
如果您参加活动课,可以尝试此操作
You can try this if you are in adapter class
and
You can try this if you are in activity class