4 Android screen adaptation solutions

Android supports multi screen mechanism, that is, it provides an appropriate way for the current device screen to jointly manage and parse application resources. This article introduces the Android screen adaptation solution in 4.

1、 Elaborate on layout_ weight

Currently the most recommended Android multi screen adaptive solution.

The function of this property is to determine the display weight of the control in its parent layout. It is generally used in linear layout. The smaller the value, the corresponding layout_ Width or layout_ The higher the priority of height, the layout is generally determined in the horizontal layout_ Priority of width; In the vertical layout, the layout is determined_ Priority of height.

Traditional layout_ Weight is used by placing the layout of the current control_ Width and layout_ Height is set to fill_ Parent, so that the display scale of the control can be completely given to layout_ weight; In this way, layout appears_ The smaller the weight, the larger the display scale. However, it is good for two controls. If there are too many controls and the display proportion is different, it will be more troublesome to control. After all, the inverse ratio is not so easy to determine.

So there is the most popular 0px setting method. Seemingly incomprehensible layout_ Height = 0px, combined with layout_ Weight, but it can make the control display in a positive proportion, which easily solves one of the most troublesome fragmentation problems in current Android development.

First look at the following styles (style_layout. XML)

As you can see, layout_ Width and layout_ The two attributes of height are encapsulated into four styles by me

According to the actual layout, choose one of them without setting it yourself. Students who have seen my previous activity group demo should be very familiar with it

Then the layout of my demo is as follows (weight_layout. XML)

2、 Custom dimension method

This is my own method. It may be a stupid method, so not many people have mentioned using this method to solve the problem of adaptation. Although this method has many disadvantages, it is sometimes a good method.

You can see that I have defined two sets of size files. We can see one of them

This is the dimensions in the values-480x320 folder_ height. In the code in the XML file, I divided the whole height into 80 equal parts, because the width or height of most screens are integer multiples of 80 (except for some special ones). Different equal parts set different size values in different resolutions.

Because each set of interface needs to be written, some students may feel that it is not very good, but this is relatively simple to write, and there is no need to change it in the future, so sometimes you can consider using it!

Let's look at the layout code of my demo (dimension_layout. XML)

The above is the unified layout code I wrote. Please note that the value of margin in my code also uses the user-defined size. If the margin uses layout_ If weight is used to control, there is no doubt that one more layer of linear layout should be nested, so no method is perfect. Sometimes the second method is more convenient to use..

3、 Setting width and height in Java code

Maybe many people will oppose this method, because even the official recommends using XML to write layout. However, we won't write so much troublesome layout code like swing, because we just reset the width and height of the control in the code, and other properties are still handed over to the XML layout file. In fact, I learned this method secretly from my colleagues. Although I don't agree with this method, it is indeed one of the solutions to the problem of screen adaptation. Moreover, it is not as complex as I thought, but it is actually very simple.

The first thing we need to do is get the width and height of the current screen, because this will be used later

We can write two static variables to save the width and height of the current screen:

Then get these two values when the first activity starts

We can uniformly write all the layout codes as wrap content. In fact, it doesn't matter what we write, because this value is only temporary

Finally, we do this in the oncreate method of the activity

4、 Multi layout

As the last method, and the last method that will be considered, that is to write the layout separately for different size interfaces. Don't use this method unless you have to. I believe many people, like me, have been forced to use this method. It should be noted that the horizontal and vertical screen switching using different layouts is also solved by this method; I won't use the code

In addition, when writing multiple layouts, this configuration code must be added to the configuration file, otherwise there may be problems sometimes

5、 Other

The above problems are all problems that need to be considered when displaying the same content on multiple screens. Another problem is that the content is displayed on different screens. In fact, this problem is often solved by rolling view, that is, scrowview; It should be noted that layout is used in crowview_ Weight is invalid. Now that you use crowview, set the size of the controls in it to fixed.

In addition, there are mainly two points about the adaptive problem of pictures. One is the 9patch graph, which everyone should learn to do. It's not difficult; However, some compilers may have such and such bugs when recognizing the 9patch diagram. For example, my eclipse does not recognize this, but the same 9patch diagram is OK on other computers,

The second problem I have been troubled by is that when 480x800 and 480x854 display the same picture, one of them will stretch (if 9patch can solve it well). In fact, what bothered me at the beginning was that the two sizes were hdpi, and I thought I couldn't make different pictures for the two screens. Later, I inadvertently found that pictures can be divided into multiple sizes as the layout, not just according to density, that is, you can write such folders as drawable-hdpi-800x480 and drawable-hdpi-854x480, and put different pictures in them, so that pictures can adapt themselves.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>