android view setbackgroundresource,java – android imageview.setBackgroundResource()不起作用
我有一个应该在点击时更改的imageviewpublic class Settings extends Activity implements OnClickListener{private ImageView im1;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState
我有一个应该在点击时更改的imageview
public class Settings extends Activity implements OnClickListener
{
private ImageView im1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
im1 = (ImageView) findViewById( R.id.imageView1 );
im1.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (v == im1 )
{
Log.d("test", "hey!");
v.setBackgroundResource(R.drawable.img1);
}
}
}
单击时,该方法运行并打印出“嘿!”但图像不会改变?
编辑:忘了提到imageview包含xml布局文件提供的另一个图像
解决方法:
按照惯例,你应该使用setImageResource(R.drawable.img1); (或setImageDrawable(getResources().getDrawable(R.drawable.img1));)而不是setBackgroundResource(R.drawable.img1);.
标签:android,java,image,mobile,imageview
来源: https://codeday.me/bug/20190930/1836979.html
更多推荐
所有评论(0)