开发者

How to assign photo in .java file to an ID in XML file for Android App

开发者 https://www.devze.com 2023-03-17 17:13 出处:网络
Im trying to assign a picture in my .java file to one of my IDs开发者_如何学Python in the xml file. Can anyone show me some code that would help me with this? ThanksLoad the proper resource file using

Im trying to assign a picture in my .java file to one of my IDs开发者_如何学Python in the xml file. Can anyone show me some code that would help me with this? Thanks


Load the proper resource file using setContentView(R.layout.layout_of_activity)

Then, make an ImageView object and use the findViewById method by passing it the ID of the image that you assigned it from the xml file, i.e.

setContentView(R.layout.main);
ImageView picture = (ImageView) findViewById(R.id.pic_id);

Then you can do whatever you want with the pic like,

picture.setImageResource(R.drawable.picture_png);

Try checking out the android api samples to see how to do some more complex image manipulation like animating or rotating

0

精彩评论

暂无评论...
验证码 换一张
取 消