Upgrade Android After studio to version 2.3, when discovering new activity or fragment, the XML layout default layout changed from Relativelayout to Constraintlayout, since Constraintlayout is already recommended, It is necessary to learn how to use it. The main goal of this paper is: The combination of graphics and text, talk about how to use Constraintlayout.
Introduction of Constraintlayout
In order to use constraintlayout, you need to add the appropriate dependencies in the App/build.gradle file:
dependencies { compile com.android.support.constraint:constraint-layout:1.0.1}
The previous Constraintlayout dependent library version number has a beta word and is now available in the official version of 1.0.1. When new activity is used with as, of course, as will automatically add this line-dependent.
Create Constraintlayout
After you create a new Constraintlayout layout file, open:
Although Constraintlayout and Relativelayout are all ViewGroup subclasses, Relativelayout,framelayout is located under the widget package, The constraintlayout is under the support package. Click "Design" and "text" in the lower left corner of the interface to toggle the display view, "Design" view is a visual view, provide drag and drop functions to achieve the interface layout; The Text view implements the interface layout by writing XML code.
For the traditional layout, most developers are writing XML code to the reality of the layout, it appears that "Design" This visual tool is quite a chicken. For Constraintlayout, however, it is well suited to use this visualizer for layout, and today the Design view is used to illustrate the use of constraintlayout. First look at the Design view,
The left area is a preset collection of controls, the top area is the action field, and the right area is the preview.
Basic Operation Interface Conversion
If the layout is not constraintlayout, you can convert by operation:
If the interface before the conversion is nested, you can choose a layer of transformations, or you can choose to convert in the root directory. When used, it is not very powerful to make sure that the conversion interface remains consistent.
Adding controls
If you want to add a button, simply drag it from the palette area on the left to the preview area. When the drag is complete, the corresponding XML code is automatically generated in the layout. The Design tool implements the layout by automatically generating code, essentially through XML. The two are interconnected, one of which changes and the other corresponds to the change. , add a button:
By dragging, a button has now been added. You can see in the Text view that the XML code is generated automatically. The button was added, but because the button has not been added yet, in the Text view, you can see that Android studio has an error: No constraints were added to the view , but still allowed to run. Because there is no constraint, the button does not know where he is, and the actual running effect is different from the preview effect, and the button appears in the upper-left corner. If you drag the button and find that the button is already constrained, then you must have opened the autoconnect switch, which is described in more detail below.
Add constraint
With the button selected, you can see four small circles around the top and bottom, move the mouse to a small circle, and the small circle will turn green. These four small circles are used to add constraints to the button, and virtually all control view has such a 4 small circle. Now add a constraint to the left and right of the button, as shown in:
Simply select the small circle and drag the mouse to the parent layout border to successfully add the constraint. , the preview on the left is called "Design", the blue view on the right is called "Blueprint", and the action to add the constraint can be done on either view.
The above constraints are constraints on the control to the parent layout, and the constraints between controls and controls are similar. Now add a button, the button below the previous button, spaced 86dp (casually), such as:
Delete Constraint
There are several ways to delete a control's constraints:
1. Remove constraints for the entire layout, such as:
2. Delete all constraints for a control: Select the control, right-click, select "Clear all Constraints", or select the control, click the X -symbol in the lower-left corner, and of course, if your entire control is deleted, the associated constraint is also deleted. Such as:
3. Delete a constraint for a control: When the control is selected, a small circle is displayed around the top and bottom of the control, the mouse is moved to the small circle, and if there is no constraint, the small circle is green; if there is already a constraint (each small circle has only one constraint), the small circle is red, Click on the red circle to delete the constraint, or drag a new constraint from the red circle and replace the old constraint. Such as:
Viewing control properties
Select the corresponding control and click "Properties" on the right to open the Properties panel, as
The more commonly used properties are displayed by default, and if you need to display more properties , scroll to the bottom and click View All properties to see all the attributes. I'm not sure if we can include all the attribute values, and if we can't find them, we can only write the children through XML. As for modifying the properties of a control, modify it directly on the panel.
The following focuses on the Inspector view on the properties panel, such as:
Let's talk about the more straightforward areas:
1.ID column: The ID that is filled in here is the ID of the identity view;
2.layout_width and layout_height: Describes the size of the control, you can directly fill in the values to determine the size, you can also choose wrap_content or Match_ Parent(These two options, in detail below), can also be dragged directly to determine the size of the control, such as:
Add a button that adds a constraint to the upper and lower left, where the button is centered by default, and the button is checked to see its inspector, as shown in:
Observe the two axes in the red box, one is vertical, one is horizontal, and the two axes are used to determine the button position. Now that the button is centered, you can see that the values identified on the two axes are all , and if you drag the button, you can see that the corresponding values change. For example, dragging a button horizontally to the right will increase the value on the horizontal axis (the range of values is between 0-100), and hold the number on the horizontal axis to drag to change the position of the button in the horizontal direction.
Now move the horizontal axis to the far right, with a value of. View preview:
Although you have moved to the far right, you find that the button is spaced from the far right. This is because the system defaults to a 8DP spacing (margin), as shown in the red box, when production constraints are set. You can change the spacing by clicking the number in the red box on the right. Observe the preview and find that each constraint has a line and a wavy line . Where the line represents the spacing (margin), the wavy line is quite the horizontal or vertical axis mentioned above, where it is dragged to change the percentage position of the button.
Each time you add a constraint to automatically add a spacing, it is also very annoying, click on the red box position can be done to kill it:
Size of the control
The layout_width and layout_heightof button are mentioned above, similar to the traditional layout, there are three kinds: fixed value ,
wrap_content and match_parent, as shown in:
1.<<< pattern indicates wrap_content;
2.|-----| patterns indicate fixed values ;
3. The wavy line pattern represents match_parent, where the match_parent is different from the match_parent under Relativelayout, The match_parent under Relativelayout is used to fill the parent layout of the current control, where the match_parent is the constraint rule that fills the current control. Assuming the layout_width is set to match_parent, the control cannot be moved horizontally, that is, the horizontal axis movement above is no longer in effect, and the control is filled with constraint rules in the preview diagram.
When layout_width or layout_height is set to match_parent , a small triangle appears in the upper-right corner, such as:
The function of this small triangle is: to toggle the aspect ratio constraints , simply to set the ratio of wide height! Such as:
If you set the aspect ratio constraint through a small triangle, and then manually change the width height value, the latter overrides the former.
With a basic understanding, let's take a look at some of the more complex points of the scene.
Snap To
Alignment constraints
To implement two buttons left-justified: Add two buttons, one on the other, add constraints from button A To button B, and add alignment constraints, such as actions:
or use Align to align: Select the control you want to align, click Align, and then select the alignment, such as actions:
When you add a left alignment constraint to button B from button A, we find that button a moves in the horizontal direction following button B.
In addition to this alignment, Android Studio offers guideline and Baseline alignment!
Consider the following scenario: if there are now two buttons, one on the other, the horizontal direction is distance from the left 30%, such as:
As we said above, this should be done: set the horizontal axis value of the two button to 30. But now I want both buttons to be 40% from the left (originally 30%), or to the right 40%. According to the original practice, only one can change the constraint next to each other, is there a better way to implement it? Guideline was born.
Guideline
Add guideline,
There are two kinds of guideline, one is the vertical direction, the other is horizontal, now look at guideline, such as:
Select guideline to display a dashed line with a left arrow at the top and a number below (assuming 80).
: 80dp from the left side of the parent layout, click the top arrow, find the arrow to the right, there is a number below (assuming 300), that is: from the parent layout to the right of 300DP; Click the top arrow again to find that the arrow becomes the % symbol, with a percentage number below (assuming 20%), Represents the position of the parent layout in horizontal direction 20%. We can drag guideline to change the value.
The role of guideline
In simple terms, the auxiliary control determines the position of the auxiliary line!
How do you implement the functions mentioned above? Add a vertical guideline, add two buttons, and set the constraints on the right side of the button to guideline, such as:
This way, the two buttons will move as you drag the guideline.
Now we have implemented different controls to guideline, moving with guideline. How to implement one control to move with another control, Baseline !
Baseline
Add Baseline
Select the button and click
, a baseline is added to the view, such as:
Add a baseline constraint to two buttons
Drag two buttons, select button A, add baseline, and drag to button B, such as:
There is only one baseline per control, and dragging from button A To button b,b automatically generates baseline, and now the two buttons have baseline constraints. We found that button A can only be dragged horizontally, cannot be dragged vertically, button B can be dragged horizontally or vertically, and button A will move in a vertical direction. The baseline is added from A to B, which means that the position of a in the vertical direction can only be determined by B, which is the alignment action of the baseline.
Automatically add constraints
There are two ways to add automatic constraints: One is autoconnect
The other is infer Constraints .
1.AutoConnect is turned off by default, and when you add a control, Android Studio detects the user's intent and automatically adds a constraint to the control. For example, dragging a button to the horizontal center of the preview will cause the as to detect a dashed line when it detects that it might be centered, and when the button is released, a constraint is automatically added to the button, as shown in:
Note that constraints are automatically added only if as detects user intent (that is, a dashed line appears in the preview), otherwise there is no constraint.
2.infer Constraints and autoconnect function Similarly, but are more powerful than the latter. AutoConnect automatically adds constraints to the controls that are being manipulated, while infer Constraints automatically adds constraints to controls throughout the layout. For more complex interfaces, create constraints for such an interface with one click!
It is important to note that the constraints that are automatically added in these two ways do not necessarily meet the requirements, and this is done by manual modification.
To add a constraint to a custom control
The Design interface runs a control that is a system preset, and what about a custom control?
You cannot find an action to drag a custom control directly into the panel, you can constrain an underlying control (such as a button), and then change the XML code to replace the button with the custom control. You also need to write the XML code manually for custom properties.
Basic use of Android constraintlayout