在Android中以编程方式更改ImageView的图像 [英] Change Image of ImageView programmatically in Android
本文介绍了在Android中以编程方式更改ImageView的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我以编程方式更改图像时,它会在最初在布局文件中设置的旧图像上显示新图像?
When I change the image programmatically, it shows new image on top of the old image which is set originally in layout file?
这是我的一小部分内容布局文件:
Here is a snippet of my layout file:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="39dp"
android:gravity="center_vertical" >
<ImageView
android:id="@+id/qStatusImage"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_margin="5dp"
android:background="@drawable/thumbs_down"
/>
<TextView
android:id="@+id/grp_child"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/radio_colors"
android:textStyle="normal"
android:background="@color/grey"
/>
</LinearLayout>
设置imageView的代码:
And the code that sets the imageView:
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//Answers
if(answersGroup != null)
answersGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// int index = answersGroup.indexOfChild(findViewById(answersGroup.getCheckedRadioButtonId()));
qImageView = (ImageView) V.findViewById(R.id.qStatusImage);
if(ans ==0 || ans == 5){
// qSV.setImageResource(0);
qImageView.setImageResource(R.drawable.thumbs_up);
}
else
qImageView.setImageResource(R.drawable.thumbs_down);
}
});
我缺少什么?
推荐答案
这是因为你设置了 ImageView
的src而不是背景。
That happens because you're setting the src of the ImageView
instead of the background.
请改用:
qImageView.setBackgroundResource(R.drawable.thumbs_down);
这里是一个讨论两种方法之间差异的线索。
Here's a thread that talks about the differences between the two methods.
这篇关于在Android中以编程方式更改ImageView的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文