如何在文本视图中以编程方式更改ImageView图像? [英] How to progmatically change the ImageView image in a textview?

查看:90
本文介绍了如何在文本视图中以编程方式更改ImageView图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有一个带有左/右图像的TextView时,如何在运行时更改它?我已经在Google上进行了一些搜索,发现了很多有关通过XML进行操作的信息,但是在代码中却做得很少.这是我尝试过的方法,但这会更改整个textview背景.我正在尝试仅更改imageview的内容.

When I have a TextView with a left/right image, how can I change that at runtime? I've googled a bit and found a lot about doing it via XML, but not much on doing it in code. This is what I tried, but this changes the entire textview background. I'm trying to just change the imageview contents.

ImageView iv = new ImageView(getBaseContext());
iv.setBackgroundResource(R.drawable.newimage);
Drawable d = iv.getBackground();
textView3.setBackgroundDrawable(d);

推荐答案

没关系-做到了.

ImageView iv = new ImageView(getBaseContext());
iv.setBackgroundResource(R.drawable.redbg);
Drawable d = iv.getBackground();
d.setBounds(0, 0, 50, 50);
tv3.setCompoundDrawables(d, null, null, null);

这篇关于如何在文本视图中以编程方式更改ImageView图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆