The method of implementing dynamic switching component background in Android _android

Source: Internet
Author: User

The program described in this paper features a dynamic selection of component backgrounds, system skins, custom toast backgrounds, etc. in software.
In order to achieve this requirement, we need to use the Sharedprefence function of Android, first write a control in the setup, set up a click Listener, click on the display of an alert selection window, let you choose, To this window again set a click on the Listener (Onitemlistener), click to a specific time, the corresponding click ID to save the sahredprefence inside, so that other places can be obtained from the settings in the selected values, dynamic personalization processing.

The specific code is as follows:

1. Set the selected action:

Scv_setaddressbg.setonclicklistener (New Onclicklistener () {
       
      @Override public
      void OnClick (View v) {
        int which = Sp.getint ("which", 0);
        Final string[] items = {"Translucent", "Vigor orange", "Guardian Blue", "Metal Ash", "Apple Green"};
        Alertdialog.builder Builder = new Builder (settingactivity.this);
        Builder.settitle ("setting attribution to show Background");
        Builder.setsinglechoiceitems (items, which, new Dialoginterface.onclicklistener () {
           
          @Override public
          Void OnClick (dialoginterface dialog, int which) {
            Editor edit = Sp.edit ();
            Edit.putint ("which", which);
            Edit.commit ();
            Scv_setaddressbg.setdesc (Items[which]);
            Dialog.dismiss ();
          }
        );
        Builder.setnegativebutton ("Cancel", null);
        Builder.show ();
      }
    );

2. Display Custom toast Action:

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.drawabl e.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.L
    Ayoutparams ();
    Params.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);
 }

3. The summary concludes as follows:

(1) To pay attention to the application of the array, ids[value] this way to use it, it is a good way and idea to write a picture resource file in an IDs array.
(2) Careful, to get the SP when the name was wrong, Config wrote configs.
(3) It is important to understand that debugging skills are based on experience and logical reasoning.

Related Article

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.