Android switch Topic 2

Source: Internet
Author: User

 

Before this article, I wrote an articleApk is a plug-in mode switch topicSo what if your boss does not accept the installation method?

Next, I will introduce another method, which is to package the resource file in the main program. The analysis of this method is as follows:

Introduction: Use different styles in the main project to load different resource files (which may sound vague and will be described in detail below)

Advantages: high speed and high efficiency. No additional installation packages are required to avoid installation, download, and other cumbersome issues. You do not need to modify the java program code, which is convenient and fast.

Disadvantage: The resource package is large and not suitable for version control. You need to re-create the OnCreate Activity for each switch.

Next, we will introduce the key technologies: 1. custom attr object: Custom attr, you must first create an attrs in the values folder. xml file, and then configure the relevant attr. This briefly introduces the meaning of attr, which is the meaning of a custom property. It is mainly used for custom controls. At the same time, we can customize it as the property name of the skin.

The specific creation is as follows:

 
         
 
The above are two custom attributes, one interface Background and one button background.

 

2. many articles on the Internet are very concise about defining a style topic and defining a topic. For unfamiliar users, they really cannot understand the topic and do not provide demos for beginners, therefore, it is easy for those with uncertain intentions to reject this method. The specific method should be:

 

 
 
      
  
  
 
I have defined two themes above: Theme. Default is the Default Theme. Ysml is the Theme of the night.

 

There are two items in two topics, namely page_bg and btn_bg. In fact, the attribute here is the attr defined above. This is not clear in the online articles, as a result, many beginners do not understand the problem or prompt errors directly here. Therefore, I would like to remind you that they hope to solve the doubts of beginners.

From page_bg and btn_bg, I have set him as two images in our resources. In fact, other types can be defined here, such as @ color @ anim. If we need to build a project, we can't write it like this. We should define additional styles for reference. Here we will not describe them in detail. thinking is very important, as long as you understand how to simplify the program, you can understand.

3. How to use our custom attributes? As follows:

 
 
  
  
 
In this layout, we used :"? Page_bg ", this is the use of image resources. Of course, when you define a color in your attribute, you can also call it directly. Here we will not explain it more. Let's put it into practice.

 

In fact, this is an implicit reference relationship, because we do not know which topic resources to call to display. How can we tell the program which topic should I display? 4. Call the topic?

Currently, there are two theme-based methods: No.1 in AndroidManifest. configure the topic in xml and set its: android: theme = attribute in an activity. However, we want to dynamically call the topic method. Therefore, this method is obviously rejected!

No. 2 is set in Activity. Method: Call the setTheme (theme) method in onCreate method of Activity. Pay special attention to the following,This method must be calledCalled before setContentView. Here I will talk about two more sentences. If we write a BaseActivity and call the setTheme method in onCreate, will it simplify our code?

 

Let's take a look at the running effect: 1. Default topic:

 

2. Blurred night themes:

 

Summary: In general, this method should be easy to accept in many companies, but I have also said its advantages and disadvantages. Maybe everyone has different ideas. Haha, the main thing is that we are familiar with the technology, when necessary, we can solve the problem immediately, which is also a good practice for our own improvement. If you have any questions, contact me.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.