Android apk achieve skinning plug-ins

Skinning ideas:

1. When skinning?

xml loaded before skinning, if xml loaded peels, users will see the color before skinning, the user experience is not good.

2. What is skin?

Skin is apk, is a resource package that contains color pictures.

3. What kind of controls should be resurfacing?

Control contains a background image, such as textView text color.

4. How to match the skin with the installed resources?

Resource names match

 

 

step:

1.xml loaded before skinning, means that the controls will need resurfacing collected. Therefore, to monitor the process xml loaded.

 1 public class BaseActivity extends Activity {
 2 
 3     SkinFactory skinFactory;
 4 
 5     @Override
 6     protected void onCreate(@Nullable Bundle savedInstanceState){
 7         super.onCreate(savedInstanceState);
 8 
 9         //监听xml生成的过程
10         skinFactory = new SkinFactory();
11         LayoutInflaterCompat.setFactory(getLayoutInflater(),skinFactory);
12     }
13 }

 

2. The need to collect skinning of controls into a container and does not change their logic directly peels (for example: do not add in each space needs resurfacing inside: "app: ......" Custom Controls Attributes)

Thinking:

(1) installation of the apk skin id id the same?

(2) picture resource, resources corresponding to the color of the automatically generated id R

Resource id (3) skin pack, id resource id and app resources in R R file if the file is the same? --are different

 

3. A plurality of control activity (SkinView) corresponding to a plurality of peels control attributes (SkinItem)

SkinItem to encapsulate these values:

  • attrName- property name (background)
  • attrValue- id attribute value in hexadecimal (@ color / colorPrimaryDark)
  • attrType-- type (color)
  • Id (id R files)
 1 class SkinItem{
 2         // attrName   background
 3         String attrName;
 4 
 5         int refId;
 6         // 资源名字  @color/colorPrimaryDark
 7         String attrValue;
 8         //  drawable color
 9         String attrType;
10 
11         public SkinItem(String attrName, int refId, String attrValue, String attrType) {
12             this.attrName = attrName;
13             this.refId = refId;
14             this.attrValue = attrValue;
15             this.attrType = attrType;
16         }
17 
18         public String getAttrName() {
19             return attrName;
20         }
21 
22         public int getRefId() {
23             return refId;
24         }
25 
26         public String getAttrValue() {
27             return attrValue;
28         }
29 
30         public String getAttrType() {
31             return attrType;
32         }
33     }

SkinView:

. 1  class SkinView {
 2          Private View View;
 . 3          Private List <SkinItem> List;   // set harvesting requires resurfacing 
. 4  
. 5          public SkinView (View View, List <SkinItem> List) {
 . 6              the this .view = View;
 . 7              the this . = List List;
 . 8          }
 . 9      }

 

4. Once gathered, skin application (xml skin during loading)

 

 

Creating SkinManager to get skin apk, app get skin apk by SkinManager

(1) Load skin package (loadSkin): AsserManager obtained by reflecting addAssetpath () method, and then the skin apk obtained by this method, thereby instantiating skinResource; then through the PackageManager. GetPackageArchiveInfo (path, PackageManager.GET_ACTIVITIES) .packageName; skin obtained Package names

(2) obtaining color (getColor): judge skinResource is empty; to get the name of the res, eg: by "colorAccent" to find the id

Guess you like

Origin www.cnblogs.com/jiani/p/11644608.html