After upgrading Android Studio 2.3, the IDE's default generated activity layout was constraintlayout as the root layout and experienced a constraintlayout that Google started pushing last year and felt very good, This article is used to document the use of constraintlayout in various aspects.
Platform Support
- Constraintlayout minimum compatible Android 2.3;
- Currently Android Studio 2.3 uses constraintlayout as the root layout of the layout file by default;
- To use constraintlayout, you need to add a com.android.support.constraint:constraint-layout:xxx version number dependency on the build.gradle of the project;
Basic use
Constraintlayout translated into Chinese is also called constrained layout, in the whole experience of the process is really through the constraints of the word, this section first to introduce some basic use, you will slowly feel constrained layout of the charm. After creating the project, open the layout file, the bottom switch Design tab, you can see the entire user interface
The top left panel is placed in the system built in a variety of controls, you want the layout to drag directly into the layout file (WYSIWYG), the right panel is selected in the layout file of the control period a variety of spatial properties, The biggest benefit of constraintlayout is that we're laying out in the form of a drag control, without worrying about the adaptation problem. So, first, drag a control to try, drag a button to the center of the screen, and then run the display to see the effect.
And actually run, but found that the button is still located in the upper left corner of the screen, a good center effect? Here we begin to introduce the constraintlayout constraint concept, we switch back to look at the XML layout code, found two problems. First, the Display center button is visible when the layout is previewed because the properties in the control property settings refer to the two tools namespaces.
As we all know, these two properties are only valid in the preview of the layout editor, and the actual run effect is not effective. Second, the button tag has a red wavy line warning, we move the mouse to the corresponding location will find the warning content, tells us that the button does not have any constraint settings, the current effect only support preview, the actual run will go back to the upper left corner, but also suggest that we should add constraints to the control.
How do I add constraints? Back to the design page, drag and drop the corresponding constraint to the left and right four points of the control.
Once you have successfully added a constraint, you will get a normal run effect.
The actual operation does not have to be in the tab, or you can drag controls directly on the text page to add constraints
After you have successfully implemented the add constraint, you can see that the button has more of the following property settings
app:layout_constraintbottom_tobottomof= "parent" means that the layout constraint at the bottom of the button is at the bottom of the parent, and the parent is the constraintlayout that wraps it. You can also set the ID of a specified control, and other similar properties will not repeat, the above four constraints together to achieve the center of the button, constraintlayout a total of the following similar properties
You will find constraintlayout very flexible to the relativelayout of the work to dry, on the left, right, top, bottom, start, end, baseline benchmark can be referenced
If I want to add one more Button2 and place it to the right of the original center button and align it with the bottom, just do the following
And you can also find that the Button2 dependency and button will follow the move of the button and then the relative movement, the purpose is to ensure that I set the dependency, always keep the Button2 on the right side of the button, and the bottom of the alignment. You can also see that the following two properties are added to the Button2 in the layout file as well
If you already understand the above-mentioned attribute meanings, there should be no doubt here.
After the introduction of the upper and lower dependent settings, the following describes some margin properties, in addition to the various common Android android:layout_marginxxx, constraintlayout self-added the following properties
These settings take effect when the dependent constraint object is set visibility to gone, it is very simple, the reader to set the practice of the comparison can be, here does not show the effect.
After the margin, let's talk about it. Bias,constraintlayout adds the following two properties to control the offset ratio of the control in the horizontal and vertical directions on the screen
When a horizontal vertical constraint is set for the target control (app:layout_constraintleft_toleftof= "parent", app:layout_constraintright_torightof= "parent" or app:layout_constrainttop_totopof= "parent", app:layout_constraintbottom_tobottomof= "parent"), this two attribute will not take effect. During the actual operation, you will find a button to set the horizontal vertical constraints drag, the layout of the Layout_constrainthorizontal_bias and Layout_constraintvertical_ The bias will always change, and if you need the button to be centered, set the parameter values of both properties to 0.5 directly.
This section introduces you to the basic use of constraintlayout.
Advanced use
After reading the basic use, and then to learn some advanced techniques, here first to add a knowledge about Constraintlayout, and other layout is different, it's layout_width and Layout_height does not support the setting of Match_parent, There are only three cases where the value of the property is evaluated as follows:
- Wrap_content;
- Specify a specific DP value;
- 0DP (Match_constraint), for the meaning of filling constraints, pay attention to not think and Match_parent is the same;
Think about what to do if you don't have a constraintlayout, and we want to make a control's width and height be laid out at a certain scale? With Constraintlayout, we can use the Layout_constraintdimentionratio property to set the aspect ratio, provided that the layout_width and layout_ of the target control The height of at least one is set to 0DP, so that a imageview width is displayed as a ratio of 2:1
Layout_constraintdimentionratio default parameter scale refers to the width: High , to high: wide can be set app:layout_constraintdimensionratio= "H,2:1".
The Constraintlayout chain (Chains) feature is very powerful, before the constraintlayout, the linear layout we mainly rely on the linearlayout to complete, with Constraintlayout, It does the work of linearlayout, for example, to line up the buttons horizontally, you can do this
So Buttona, B, C form a chain in the horizontal direction, and the bottom is aligned. Go back to the XML file and meet the Buttona new App:layout_constrainthorizontal_chainstyle property setting, which will only appear in the first control in a chain. This control is the head of the entire chain.
In addition to the horizontal layout_constrainthorizontal_chainstyle, there are vertical layout_constraintvertical_chainstyle, both of which have Spread,spread_ Inside,packed these three kinds of values, if you set the control's Layout_width and layout_height to 0DP, you can also mate Layout_constrainthorizontal_weight, Layout_ Constraintvertical_weight Two property implementations and LinearLayout set layout_weight the same effect, the specific operation is no longer shown here, the following picture tells you the strength of chain.
About chain Introduction to this, the last part of the advanced guideline function, if we need to layout the central axis of the screen, you can use the guideline to operate, such as the following two buttons are distributed on the left and right sides of the central axis
From the operation we can see that the guideline is also divided into vertical and horizontal two, and support settings in the screen location, you can use Layout_constraintguide_begin and Layout_constraintguide_ End to set the specific DP value, or you can use Layout_constraintguide_percent to set the scale. In fact, it is only an auxiliary layout of the view, its internal implementation of the source code is also very simple, and the default setting of visibility to gone, about Constraintlayout advanced use of the introduction to this.
Use Advantage
With regard to the use advantage of constraintlayout, my personal experience is summarized as follows:
- Efficient layout, Android so many years in the real sense of the realization of the resulting drag pattern layout, greatly improve the development efficiency;
- Easy and flexible implementation of complex layouts;
- To solve multiple layout nesting problems, you will find that constraintlayout is really very flexible and can avoid nesting between layouts to a great extent.
- To meet the needs of screen adaptation, think of no constraintlayout before the drag-type layout, you know how disgusting;
Best practices
Constraintlayout in the beginning there are many developers looking to completely rely on the drag-and-drop way to achieve the layout, and in the actual operation, completely by dragging in fact, the efficiency is a compromise, this recommendation is the drag method and XML coding combination is the best practice way.
This article is the technical Horizon original works, reproduced please indicate the original source: Http://blog.coderclock.com/2017/04/09/android/android-constraintlayout, Welcome to my public number: Technology Horizon
Android Constraintlayout User Guide