Android implements a method for dynamically switching component backgrounds

  • 2020-06-03 08:19:35
  • OfStack

The functions of the program described in this article are to dynamically select component backgrounds, system skins, and custom toast backgrounds in the software.
To achieve this one request, you need to use the android SharedPrefence function, first write a control in setting, setting a click listener, click show 1 Alert choose play a window, let you choose, for this popup window to set up a click listener (onItemListener), click on to a specific time, the corresponding click save into sahredprefence id, so, other places can be obtained from here the value of the Settings in the options for dynamic personalized treatment.

The specific code is as follows:

1. Set selected operation:


scv_setAddressBg.setOnClickListener(new OnClickListener() {
       
      @Override
      public void onClick(View v) {
        int which = sp.getInt("which", 0);
        final String[] items = {" translucent "," Vibrant orange "," The guard is blue "," Metal gray "," Apple green "};
        AlertDialog.Builder builder = new Builder(SettingActivity.this);
        builder.setTitle(" Sets the home display 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 the operation of custom toast:


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();
    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 conclusion is as follows:

(1) Pay attention to the application of arrays, ids[value] this use should be able to think of using, the image resource file in an ids array is a good way and idea.
(2) Careful, when I got the name sp, I made a mistake. config was written as configs.
(3) It is important to understand that debugging skills are empirical and logical.


Related articles: