Example of android using SkinManager to implement skin changing function

  • 2021-08-12 03:45:53
  • OfStack

Try to use SkinManager written by Hongyang Great God to realize the skin changing function.

1. Configure

Add dependencies in build. gradle under app:


// Skin changing function 
compile 'com.zhy:changeskin:4.0.2'

This is configured, and then initialized in the program entrance.

2. Global initialization

In the class you created that inherits application, add:


// Skin change sdk Initialization 
SkinManager.getInstance().init(this);

This class must be in the manifest file < application/ > Node configuration.

Next, you need to register.

3. Registration

Add permissions to the manifest file:


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Register on the page onCreate () where the skinning function is used:


// Skinning function page registration 
SkinManager.getInstance().register(this);

Of course, there is registration and cancellation. Cancel registration in onDestroy method:


// Skin change function cancellation 
SkinManager.getInstance().unregister(this);

4. Naming rules

1. The naming rule of SkinManager is: prefix + "_" + suffix;

2. The naming of prefixes and suffixes is defined by developers themselves;

3. There are 1 prefix with different skin colors and 1 prefix with different attributes;


// For example: Defining textColor Property value is @color/skin_text Different skin colors color The naming prefixes are all skin_text

4. Suffixes are configured according to skin color themes, and all attributes of each theme have 1 suffix;


// For example, skin color has two themes: black and white. If black and white is defined as " black " white ", 
// So whether it's textColor Or background The suffix that belongs to the black theme is black The suffix that belongs to the white theme is white . 

5. Skin color configuration

1. Configure different skin colors in res/values/colors. xml:


<!-- White characters on black background (default theme) -->
<color name="skin_text">#FFFFFF</color>
 
<!-- White characters on black background (black theme) -->
<color name="skin_text_black">#FFFFFF</color>

 <!-- Black characters on white background (white theme) -->
<color name="skin_text_white">#000000</color>

2. Create background shape under res/drawable:

skin_bg. xml (Default theme)


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">

 <solid
  android:color="#000000"/>
</shape>

skin_bg_black. xml (Black theme)


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">

 <solid
  android:color="#000000"/>
</shape>

skin_bg_white. xml (white theme)


// Skin change sdk Initialization 
SkinManager.getInstance().init(this);
0

3. Import pictures with different themes under src/drawable:

//The pictures are also named after skin_src.png, skin_src_white.png, skin_src_ES100png

Note: These three skin color configurations are not required. 1 is commonly used to configure textColor attribute, 2 is commonly used to configure background attribute, and 3 is commonly used to configure src attribute, which can be set according to the project needs

6. Layout references

References in the layout directly set the tag property.

textColor property settings:


// Skin change sdk Initialization 
SkinManager.getInstance().init(this);
1

background property settings:


// Skin change sdk Initialization 
SkinManager.getInstance().init(this);
2

src property settings:


<ImageView
   style="@style/WrapWrap"
   android:src="@drawable/skin_src"
   android:tag="skin:skin_src:src"/>

Note: With SkinManager, the @ color setting is not supported for the background property, only @ drawable/...

7. tag Attributes

It is divided into three parts:

Part 1 skin is a fixed value and cannot be changed;

Part 2 skin_text, skin_bg, skin_src are skin color configuration prefixes;

Part 3 textColor, background and src are the attributes corresponding to skin color configuration;

In addition, tag attributes support multi-attribute configuration, which is divided by "", such as:


// Skin change sdk Initialization 
SkinManager.getInstance().init(this);
4

Both textColor and background properties are set.

Note: Part 2 (skin_text) does not have to be exactly the same as the naming (@ color/skin_text) of the corresponding attribute value (android: textColor), because I set skin_text as the default theme. If you set skin_text_default as the default theme, the tag configuration remains unchanged and the attribute value should be @ color/skin_text_default

8. Skin change code

With the TextView click event in item 7 as the layout, in the page:


// Skin change sdk Initialization 
SkinManager.getInstance().init(this);
5

The skin changing function is realized by using changeSkin ("suffix");

If you want to add skin change function in the pop-up window, you can call SkinManager. View view of SkinManager () in the page code after adding skin change configuration in the pop-up window layout.


// Skin change sdk Initialization 
SkinManager.getInstance().init(this);
6

Note: sp cache function is added to SkinManager, which will cache the suffix of skin color configuration. If there is any problem in the test configuration process, clean up the cache first and try again.


Related articles: