官术网_书友最值得收藏!

Applying constraints

The best way to see how this works is to try it, as these few simple steps will demonstrate. Create a ConstraintLayout as described previously and drag and drop one or two views or widgets from the palette onto the blueprint screen, similar to following figure:

Constraint handles

Each view has constraint handles on its corners and its sides. Those on the corners are for simply resizing a view in the way that we are all familiar with. Those along the sides, however, are used to create constraints. These position views are relative to their parents or each other in a way that is not dissimilar to the relative layout.

As this is largely a graphical form of editing, it is best demonstrated by action. Drag the left side anchor point of one of your views toward the left side of the layout and release the mouse button as prompted to create a parent constraint. This being a layout that contains others will be a parent constraint.

As you experiment with constraints, you will notice how the margins automatically stick to those values recommended by the creative design guidelines.

If you now open the text editor, you will see the constraint realized like so:

app:layout_constraintLeft_toLeftOf="parent" 

You will also notice from the code that an error is generated by this view. This is because we need both vertical and horizontal constraints for each view. This is achieved in exactly the same way, with something like:

app:layout_constraintTop_toTopOf="parent" 

Constraints can be created between child views as well, using the same drag and drop technique or:

app:layout_constraintTop_toBottomOf="@+id/image_view" 
Setting a constraint on all four sides of a view will center it in its container.

Constraints can be used to align sibling views as well as joining two adjacent sides, which generates code along these lines:

app:layout_constraintLeft_toLeftOf="@+id/image_view" 

A constraint can be deleted very simply, in either editor mode, by clicking on its originating handle.

This drag and drop method is not peculiar to Android Studio, but there is one editing tool that is unique to Android Studio by providing a editable schematic perspective in the Properties tool.

主站蜘蛛池模板: 辽源市| 湖北省| 祁门县| 鹤峰县| 凤台县| 清流县| 乌恰县| 新干县| 嵩明县| 大姚县| 阳泉市| 民丰县| 郯城县| 宕昌县| 沂南县| 元谋县| 两当县| 永泰县| 水城县| 南部县| 台江县| 辽阳县| 湖北省| 呼图壁县| 北川| 会理县| 赣州市| 双辽市| 洛隆县| 新昌县| 达州市| 葵青区| 辛集市| 华坪县| 孟津县| 眉山市| 灵璧县| 平阴县| 墨玉县| 徐州市| 连州市|