Try to use SkinManager written by Master Hongyang to implement the skin removal function.
1. Configuration
Add dependencies under the app:
//Skin Removal Functioncompile ':changeskin:4.0.2'
This is configured and then initialized in the program entrance.
2. Global initialization
Add in the class that you created to inherit application:
//Skin Removal SDK Initialization().init(this);
This class must be configured in the manifest file <application/> node.
Registration is required next.
3. Register
Add permissions to the manifest file:
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name=".READ_EXTERNAL_STORAGE"/>
Register in the page onCreate() using the skinning function:
//Register skin removal function page().register(this);
Of course there is a registration, and you will cancel it. Unregister in the onDestroy method:
//Skin Removal Function Logout().unregister(this);
4. Naming rules
1. The SkinManager naming rules are: prefix + "_" + suffix;
2. The naming of prefixes and suffixes is defined by the developer;
3. The prefixes of different skin colors of the same attribute are the same, and the prefixes of different attributes are different;
//For example:definitiontextColorThe attribute value is@color/skin_text,Different skin tonescolorAll named prefixes areskin_text
4. The suffix is configured according to the skin color theme, and all attribute suffixes for each theme are the same;
//For example: There are two themes of skin color: black and white. If black and white are defined as "black" and "white",//Then no matter whattextColorstillbackgroundThe suffix belongs to the black theme isblack,The suffix belongs to the white theme iswhite。
5. Skin color configuration
1. Configure different skin colors in res/values/:
<!--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 letters on white background(White theme)--> <color name="skin_text_white">#000000</color>
2. Create a background shape under res/drawable:
skin_bg.xml (default topic)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:andro android:shape="rectangle"> <solid android:color="#000000"/> </shape>
skin_bg_black.xml (black theme)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:andro android:shape="rectangle"> <solid android:color="#000000"/> </shape>
skin_bg_white.xml (white theme)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:andro android:shape="rectangle"> <solid android:color="#FFFFFF"/> </shape>
3. Import different theme pictures under src/drawable:
//The picture is also named after skin_src.png, skin_src_white.png, skin_src_black.png
Note: These three skin color configurations are not necessary. 1 is often used to configure the textColor attribute, 2 is often used to configure the background attribute, 3 is often used to configure the src attribute, and set it yourself according to the project needs.
6. Layout reference
The reference in the layout directly sets the tag attribute.
TextColor property settings:
<TextView android: style="@style/MatchWrap" android:layout_marginTop="@dimen/dp_10" android:gravity="center_horizontal" android:tag="skin:skin_text:textColor" android:text="Test text" android:textColor="@color/skin_text" android:textSize="@dimen/sp_24" />
Background property settings:
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/skin_bg" android:tag="skin:skin_bg:background">
src attribute settings:
<ImageView style="@style/WrapWrap" android:src="@drawable/skin_src" android:tag="skin:skin_src:src"/>
Note: Using SkinManager, the background property does not support @color setting, only @drawable/...
7. Tag attributes
It is divided into three parts:
The first part skin is a fixed value and cannot be changed;
The second part skin_text, skin_bg, and skin_src are skin color configuration prefixes;
The third part textColor, background, and src are the properties corresponding to the skin color configuration;
In addition, the tag attribute supports multi-attribute configuration and is divided by "|", such as:
<TextView style="@style/MatchWrap" android:layout_marginTop="@dimen/dp_10" android:background="@drawable/skin_bg" android:gravity="center_horizontal" android:onClick="changeClick" android:tag="skin:skin_text:textColor|skin:skin_bg:background" android:text="Test text" android:textColor="@color/skin_text" android:textSize="@dimen/sp_24" />
The textColor and background properties are set at the same time.
Note: The second part (skin_text) does not have to be exactly consistent with the corresponding attribute value (android:textColor) name (@color/skin_text). This is because the skin_text I set is 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 Refill Code
Use the TextView click event in item 7 to achieve skinning as the layout, in the page:
//Declare member variables, default false, default theme is blackprivate boolean isChange; //Implement setting click eventpublic void changeClick(View view){ (TAG, "Click to change"); isChange = !isChange; if(isChange){ ().changeSkin("white"); }else{ ().changeSkin("black"); } }
Use changeSkin("suffix") to achieve skin removal;
If you want to add skinning function to the pop-up window, you can call the injectSkin(View view) method of () in the page code to implement it after adding skinning configuration in the pop-up window layout.
final Dialog dialog_skin = new Dialog(activity, ); View contentView = (activity, .dialog_skin, null); //Implement dialog box skinning().injectSkin(contentView); DialogSkinBinding binding = (contentView); dialog_skin.setCanceledOnTouchOutside(true); dialog_skin.show();
Note: SkinManager has a sp cache function, which will cache the skin color configuration suffix. If there is any problem with the test configuration process, clean the cache first and try.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.