Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change the skin of Android APP

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to change the skin of Android APP". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "how to change the skin of Android APP" together.

Background requirements

Android APP skinning can be divided into two categories:

Switch between two sets of themes (i.e. day/night) using a single switch button.

Multiple themes are downloaded and updated online.

The first implementation basically uses the set local Theme to operate, that is, all the resources packaged into the APP, and switch according to the theme. The second implementation is impossible to use because packaging all resources into an APP lacks flexibility, discourages activity updates, and also makes the apk package larger. All implementations of the second type must support online downloads.

scheme selection

To meet the needs of the product and to achieve the flexibility of skin change, we choose the second option mentioned above. After previous discussions between Android and IOS members, the consensus felt that it could be replaced by downloading compressed packages and reading resources by parsing compressed packages.

How do I access resources after downloading compressed packages? There are two ways:

Decompress the downloaded skin package and read the image resources and file resources in the file stream.

Load the downloaded skin package into assetManager, and create a Resource object through the manager. The control that needs to be skinned reads the resource through the Resource object.

The first method requires manual file streaming, and different file streams have different file streaming methods, such as images, text files, etc., and different devices load different resources due to resolution. How to reasonably select the appropriate resources to load is also a problem that needs to be solved.

The second way needs to load the skin package into the assetManager manager. The Resource object newly generated by the assetManager manager and the Resource object of our main project are different objects of the same class. We can load resources in a familiar way (such as resource.getColor, resource.getDrawable, etc.).

Based on the above two ways of loading resources, the second way is selected here to load and read resources.

specific implementation

1. Download the required skin package to the local area through the network. The skin package here is an apk file. In order to make the apk package small enough, it only contains resource files. There may be multiple skin packs, such as theme1.skin, theme2.skin...

2. Get the name of the skin package to be loaded by background, such as theme1.skin, and generate a new Resource object by calling addAssetPath method of AssetManager object, as follows:

AssetManager assetManager = AssetManager.class.newInstance(); //Because addAssetPath() method is hidden, it cannot be accessed directly using objects.//Reflection method is used here, which adds the skin package to asset manager Method addAssetPath = assetManager.getClass().getMethod("addAssetPath" , String.class); addAssetPath.invoke(assetManager, skinPath); Resources skinResource = new Resources( assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());

3. Customize a subclass of InflaterFactory, SkinInflaterFactory, override onCreateView(View, String, Context, AttributeSet) method, parse and store attributes of controls that need to be skinned, and then load resources into Resource objects in step 2 for these skinned controls and set them to these controls.

4. Create a new SkinInflateFactory object in the onCreate method of BaseActivity, and set the SkinInflateFactory object to the LayoutInflater object of Activity, as follows:

protected void onCreate(@Nullable Bundle savedInstanceState) { mSkinInflaterFactory = new SkinInflaterFactory(); LayoutInflaterCompat.setFactory( getLayoutInflater(), mSkinInflaterFactory); super.onCreate(savedInstanceState); } Flowchart

other issues

1. How to trigger different business processes after clicking the control?

You can customize an attribute, such as skin:click="@string/clickAction", clickAction="muapp://app/testDefault" in the main project, clickAction="muapp://app/testClick" in the skin package, and trigger different jump actions through the routing mechanism in the current project. For example, the above default jump is to jump to the invoke method of TestDefaultAction (actionName="testDefault") class of the main project (app is module name), and after changing, it will jump to the invoke method of TestClickAction (actionName="testClick") class of the main project (app is module name).

2. How to support different behaviors of controls? For example, different animation effects, etc.

This problem is handled in a similar way to the first problem, and can also be handled in different ways by different tags (String copy) in the main project and skin package.

3. How to deal with the skin changing requirements of custom View?

You can add a method to pass the attribute name (such as background) and attribute value (such as the resource ID of the image corresponding to background) of the custom View to the method, and then go to the Resource object of the skin package to find if there is a corresponding replaceable skin or replaceable behavior.

Thank you for reading, the above is the content of "Android APP how to change skin", after the study of this article, I believe everyone has a deeper understanding of how to change skin of Android APP, the specific use situation still needs everyone to practice verification. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report