Enable dynamic switching of component backgrounds in Android

Source: Internet
Author: User

This is also learned yesterday, summed up the idea, because this knowledge point will definitely be used again.

Purpose: I want to dynamically select the component background, system skin, custom toast background and so on in the software.

Implementation of the idea: to use the sharedprefence in the Android function, in the settings to write a control, set a click Listener, click on the display of an alert select pop-up window, let you choose, to this pop-up window and then set a click Listener (Onitemlistener), Click to the specific time, the corresponding click ID to save the sahredprefence inside, so that the other places can be obtained from the settings in the selected values, dynamic personalized processing.


Specific code:

To set the selected action:

Scv_setaddressbg.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {int which = Sp.getint ( "which", 0); final string[] items = {"Translucent", "Vitality Orange", "Guardian Blue", "Metallic Grey", "Apple green"}; Alertdialog.builder builder = new Builder (settingactivity.this), Builder.settitle ("Set the attribution to show background"); Builder.setsinglechoiceitems (items, which, new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Editor edit = Sp.edit (); Edit.putint ("which", which); Edit.commit (); scv_ Setaddressbg.setdesc (Items[which]);d Ialog.dismiss ();}); Builder.setnegativebutton ("Cancel", null); Builder.show ();}});


To display custom toast operations:

public void Showmytoast (String address) {<span style= "color: #ff6600;" >int[] ids = {R.drawable.call_locate_white,r.drawable.call_locate_orange,r.drawable.call_locate_blue, R.drawable.call_locate_gray,r.drawable.call_locate_green};</span><span style= "color: #ff6600;" >sharedpreferences sp = getsharedpreferences ("config", mode_private); int which = Sp.getint ("which", 1); </span >view = View.inflate (this, r.layout.address_show, null); TextView TextView = (TextView) View.findviewbyid (r.id.tv_address); Textview.settext (address); <span style= "color:# ff6600; " >view.setbackgroundresource (Ids[which]); </span>windowmanager.layoutparams params = new    Windowmanager.layoutparams ();p arams.height = WindowManager.LayoutParams.WRAP_CONTENT;    Params.width = WindowManager.LayoutParams.WRAP_CONTENT; Params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCReen_on;    Params.format = pixelformat.translucent; Params.type = Windowmanager.layoutparams.type_toast;wm.addview (view, params);}


Summarize:

1. To be aware of the application of the array, ids[value] this way to use, it is a good way to write the picture resource file in an IDs array.

2. Careful, get SP when the name is wrong, Config wrote Congig.

3. It is important to understand that debugging skills are based on experience and logical reasoning.

Enable dynamic switching of component backgrounds in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.