UI adaptation solution for Android tablets
Because of the fragmentation of The Android system, adaptation has become an increasing headache for developers, and this article’s adaptation is UI adaptation. Because it is a new project development, the final use of toutiao adaptation scheme, low intrusion, good effect, can easily modify the parameters to achieve the purpose of adaptation design.
Common adaptation schemes
A, width and height qualifier adaptation
meaning
In the res folder, create a number of values- folders and calculate the corresponding dimen values, similar to the following directory
├ ─ ─ SRC/main │ ├ ─ ─ res │ ├ ─ ─ ├ ─ ─ values │ ├ ─ ─ ├ ─ ─ values - 800 x480 │ ├ ─ ─ ├ ─ ─ values - 860 x540 │ ├ ─ ─ ├ ─ ─ values - 1024 x600 │ ├ ─ ─ ├ ─ ─ values - 1024 x768 │ ├ ─ ─ ├ ─ ─... │ ├ ─ ─ ├ ─ ─ x1440 values - 2560Copy the code
disadvantages
This scheme has a fatal defect, that is, it needs accurate hit to adapt, the error tolerance rate is poor; And we also have a lot of trouble calculating dimen, although there is an automatic generation tool.
2. SmallestWidth qualifier adaptation
meaning
Similar to the first method, it solves the problem of fault tolerance of the first method. Similar to the resources in MIPMAP, it will automatically find the resource files similar to its own. Mr. Radin Wu strongly supports this method in his article.
├ ─ ─ SRC/main │ ├ ─ ─ res │ ├ ─ ─ ├ ─ ─ values │ ├ ─ ─ ├ ─ ─ values - sw320dp │ ├ ─ ─ ├ ─ ─ values - sw360dp │ ├ ─ ─ ├ ─ ─ values - sw384dp │ ├ ─ ─ ├ ─ ─ values - sw400dp │ ├ ─ ─ ├ ─ ─... │ ├ ─ ─ ├ ─ ─ values - sw600dpCopy the code
Three, hongshen’s AndroidAutoLayout
meaning
Let your Activity inherit from AutoLayoutActivity and specify the size of your design drawing in the AndroidManifest. When writing your layout file, you can write px directly, and the framework will automatically convert to px
<meta-data android:name="design_width" android:value="768">
</meta-data>
<meta-data android:name="design_height" android:value="1280">
</meta-data>
Copy the code
disadvantages
We can think of, because the framework to do change at run time in onMeasure, our custom controls may be affected or restricted, there may be some specific controls, requires a separate adapter, there may exist the dark pit is unpredictable, there is a more important question, that is the whole adaptation is a framework to complete, Once this framework is used, it will be very troublesome to replace problems that are difficult to solve in the future. Moreover, once the maintenance of the project stops, the follow-up upgrade will have to be done by yourself. Can the team afford this cost? Maintenance has been stopped
Iv. Toutiao adaptation scheme
meaning
All adaptation problems are solved by changing the density to force the width DP value to a uniform value for all phones with different sizes and resolutions.
The problem
- When using some third party views, because the third party View is not designed according to the size of our UI, so it will cause a deviation.
We can actually reduce the use of third party View, or use a more comprehensive resolution of the third party View.
-
It is normal to use a custom dialog, but when using the system’s own dialog, the display may be incomplete. In this case, you need to change the size of the dialog.
Solution: blog.csdn.net/u011368551/…
Implementation method
app/build.gradle
Implementation 'me. Jessyan: autosize: 1.1.2'Copy the code
Manifest.xml application tag
<meta-data
android:name="design_width_in_dp"
android:value="853" />
<meta-data
android:name="design_height_in_dp"
android:value="553" />
Copy the code
Five, the use of toutiao adaptation solution problems summary, continuous update.
1. A problem
- A scenario
In landscape applications, when you press the home button or lock the screen and the software enters the background, keep the portrait screen of the phone. At this time, new fragments are generated to update the UI, which may cause adaptation errors.
- Cause analysis,
When the screen is locked or the background is entered, although the application is fixed in landscape, the phone will let the software get the portrait state when the phone is in portrait state, so that when the new UI is generated, the adaptation effect of opposite width and height will be generated.
- The solution
The solution to the Issue: github.com/JessYanCodi…
My solution:
In the Activity
@override public Resources getResources() {// AutoSizeCompat int width = AutoSizeConfig.getInstance().getScreenWidth(); int height = AutoSizeConfig.getInstance().getScreenHeight(); AutoSizeCompat.autoConvertDensity(super.getResources(), 961, width > height); return super.getResources(); }Copy the code
2. Question 2
- A scenario
When using the system’s own dialog, the display may be incomplete.
- Cause analysis,
Since you are changing the density of the system, the system view will also be adapted.
- The solution
Change the native Dialog size and set it after the Dialog executes the show method, otherwise it is invalid
if (mAlertDialog.getWindow() ! = null) { WindowManager.LayoutParams lp = mAlertDialog.getWindow().getAttributes(); lp.width = 300; Lp. gravity = gravity.CENTER; mAlertDialog.getWindow().setAttributes(lp); }Copy the code