How to Develop the "AA money Calculation Software" for Android Developers (III)
Today, we added a skin replacement function. You can switch between 6 different backgrounds, that is, click the "skin replacement" button to switch the background image.
I have searched the internet for a long time. The most reliable and understandable method is the following link: Click to open the link.
It took me a long time to add this function, so I began to doubt if I had a problem in this development? Because I have not studied Android development in the system, and many details are unclear, will it become more and more difficult .. Could you tell me this new guy.
Okay. Let's talk about the detailed implementation process of this function! Code on!
The core code is as follows:
Resources res = getResources();Drawable drawable = res.getDrawable(R.drawable.newImage); LinearLayout linearLayout = (LinearLayout)findViewById(R.id.bg);linearLayout.setBackgroundDrawable(drawable);
The code in main. xml is:
// The subsequent values are long and irrelevant.
We can see that the original background is bg.png, And the last change is newimage.png. Previously, I used View to switch the background on the Internet, but there was always only one small image to be switched. So I think it would be best to use linearLayout ~
After that, I implemented the context switching function, that is, each time I click the "skin change" button, a new image will be switched. A total of six images will be placed under the drawable-hdpi Folder:
The core code is as follows:
Public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); super. setContentView (R. layout. main); this. popbut = (Button) super. findViewById (R. id. popbut); this. statusinfo = (TextView) super. findViewById (R. id. statusinfo); this. popbut. setOnClickListener (new OnClickListenerImpl (); Button skinButton = (Button) findViewById (R. id. skinbut); skinButton = (Button) super. findViewById (R. id. Skinbut); skinButton. setOnClickListener (new SkinOnClickListenerImpl (); button1 = (Button) findViewById (R. id. but); button1.setOnClickListener (new Button. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubnew AlertDialog. builder (AAcharge. this ). setTitle ("about "). setMessage ("This is MSJ's first Android APP, I hope you like it, have fun! "). SetPositiveButton ("OK", null ). show () ;}});} private class SkinOnClickListenerImpl implements OnClickListener {@ SuppressWarnings ("deprecation") @ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubResources res = getResources (); int skinid = 0; if (skinID <= 6) {skinid = getResources (). getIdentifier ("bg" + skinID, "drawable", getPackageName (); Drawable drawable = res. getDrawable (skinid); LinearLayout linearLayout = (LinearLayout) AAcharge. this. findViewById (R. id. bg); linearLayout. setBackgroundDrawable (drawable); skinID = skinID + 1;} else {skinID = 0; skinid = getResources (). getIdentifier ("bg" + skinID, "drawable", getPackageName (); Drawable drawable = res. getDrawable (skinid); LinearLayout linearLayout = (LinearLayout) AAcharge. this. findViewById (R. id. bg); linearLayout. setBackgroundDrawable (drawable); skinID = skinID + 1 ;}}}
Thank you for reading! Thank you for your comments!