Explore Android Constraintlayout layouts

Source: Internet
Author: User

Original link: https://medium.com/exploring-android/exploring-the-new-android-constraintlayout-eed37fe8d8f1#.r4tmej1ut

In last week's Google I/O, a lot of great Android-related news was introduced. While I like to sit here and discuss all the things released, but we are going to quickly browse the new exciting Android Constraintlayout.

To better understand constraintlayout, I use one of my existing projects (Bourbon) and manually use the Constraintlayout to transform each layout. Check out items from GitHub at any time!

Configuration

Before you start, you need to do something if you want to try a new constraintlayout.

    • First, you need to download the Android Studio 2.2 preview from here .
    • You also need to add constraintlayout dependencies from the support library.
compile      ‘com.android.support.constraint:constraint-layout:1.0.0-alpha1‘

All right, you should be ready now!

So, what is Constraintlayout?

Constraintlayout is a powerful new class that can imagine a relativelayout--on steroids, which is constraintlayout. It allows us to use a ' constraint ' to place a child view to define a position based on the relative relationship between different views in the layout.

The goal of Constraintlayout is to help reduce the number of nested views, which can improve the performance of layout files. The layout class also makes it easier to define layouts than when using relativelayout, because we can now pin any side of one view to the other, without having to prevent the entire view from going to any other side.

For example, a relative layout's properties allow us to place a view:

    • Layout_torightof
    • Ayout_toleftof
    • Layout_totopof
    • Layout_tobottomof

However, Constraintlayout is used for more features:

  • layout_constrainttop_totopof?—? expect the top of the view to align to the top of the other view.
  • layout_constrainttop_tobottomof?—? expect the top of the view to align to the bottom edge of the other view.
  • layout_constrainttop_toleftof?—? expect the top of the view to align to the left of the other view.
  • layout_constrainttop_torightof?—? expect the top of the view to align to the right of another view.
  • layout_constraintbottom_totopof?—? expect the bottom of the view to align to the top of the other view.
  • layout_constraintbottom_tobottomof?—? expect the bottom edge of the view to align to the bottom edge of the other view.
  • layout_constraintbottom_toleftof?—? expect the bottom edge of the view to align to the left side of the other view.
  • layout_constraintbottom_torightof?—? expect the bottom edge of the view to align to the right side of the other view.
  • layout_constraintleft_totopof?—? Expect the left side of the view to align to the top of the other view.
  • layout_constraintleft_tobottomof?—? Expect the left side of the view to align to the bottom edge of the other view.
  • layout_constraintleft_toleftof?—? expect the left side of the view to align to the left side of the other view.
  • layout_constraintleft_torightof?—? expect the left side of the view to align to the right of another view.
  • layout_constraintright_totopof?—? expect the right side of the view to align to the top of the other view.
  • layout_constraintright_tobottomof?—? expect the right side of the view to align to the bottom edge of the other view.
  • layout_constraintright_toleftof?—? expect the right side of the view to align to the left side of the other view.
  • layout_constraintright_torightof?—? expect the right side of the view to align to the right side of the other view.
  • If required, the properties support start and end are also available for left and right alignment.

Yo, a lot of attributes, right? These properties provide a great deal of control over the position of the inside view of the constraintlayout.

Note : Constraintlayout is also backwards compatible to API 9??

Constraint handling

Constraints are essentially rules that are defined on the view, where the rule declares the location of the views and on-screen alignment.

Sizing processing

By using the resize feature in the layout editor, we can change the size of the selected view, resize the view, and set constraints on the new dimensions.

Adjust the view dimensions use the adjustment anchor points in the layout editor to automatically re-calculate constraints that were previously set on the view.

Edge constraint Processing

Boundary constraint processing can be used to specify the location of a view within a layout. For example, by setting a preset boundary DP value, these anchor points can be used to define the selected view that always appears to the left of another view.

You can set it in the layout editor by dragging the anchor point of the view to the boundary of another view.

or set the desired view properties in the XML layout:

app:layout_constraintRight_toRightOf="@+id/text_like_count"
Baseline constraint Processing

Baseline limits are allowed to be aligned with the baseline of multiple textview, regardless of their text size.

In the layout editor, set by dragging and dropping the baseline anchor point of the desired textview to the baseline of another textview.

or set the desired view properties in the XML layout file:

app:layout_constraintBaseline_toBaselineOf="@+id/text_title"

Note : Baseline constraint processing can only be constrained to another baseline.

Vertical offset

This allows us to use offsets to place the view vertically, which is relative to its constraint position.

We can use the slider in the Layout editor (see) or the property settings in XML:

app:layout_constraintVertical_bias="0.5"
Horizontal offset

This allows us to use the offset to place the view horizontally, and certainly relative to its constraint position.

We can use the slider in the Layout editor (see) or the property settings in XML:

app:layout_constraintHorizontal_bias ="0.5"
View Zoom

In the Properties section of the Layout editor, you notice that a part shows the actual dimension constraints of the selected view.

Any size

In this block area, you can see how those wavy lines actually represent how the view is scaled in the layout. The line above indicates that the view size will adhere to the constraint, meaning it will match_parent and use the available space.

Matching content


These lines now indicate that the view's height and width will fit the view's content, i.e. it expands only as needed.

Fixed size


Finally, these lines indicate that the width and height of the view are a fixed size in DP.

Automatic connection

Automatic connection allows you to place the view in the layout editor and automatically calculate and set constraints on the view-how convenient!

Use Automatic connection:

Manual constraints

If you want to manually set constraints in the layout editor, automatic connections may be disabled. Personally, this makes the constraints more manageable and saves time for deleting constraints.

In order to use manual constraints, you need to disable automatic connections:

Reasoning

Similar to the behavior of automatic connections, inference can be used to automatically (and magically) create constraints between multiple views in a layout. Unlike automatic connections, inference calculates constraints on all views in the layout file, not just the selected view.

Delete Constraint

In the layout editor, you can easily delete a view's constraints. There are two ways of doing this.

Individual restrictions

Simply click on the anchor point of the constraint you wish to delete, and you can remove the individual limit.

All constraints

When the view is selected, a small icon appears in the lower-left corner-clicking on this icon will remove all constraints set by the view.

Example

Take a look at one of the layouts in my constraint sample project. This enables the layout of the Dribbble shooting Project , which is shown in the layout editor as follows:

You can see the constraints that have been set, which are:

    • The picture that appears at the top of the view has a constraint on the upper and lower left side of the other parent container. This way the view is placed in the center of the parent container.
app:layout_constraintBottom_toBottomOf=”@+id/constraintLayout”app:layout_constraintEnd_toEndOf=”@+id/constraintLayout” app:layout_constraintStart_toStartOf=”@+id/constraintLayout” app:layout_constraintTop_toTopOf=”@+id/constraintLayout”
    • The ' Japan ' text view aligns to the left edge of its container and also aligns to the left side of the heart-shaped picture-so that they do not overlap.
app:layout_constraintBottom_toBottomOf=”@+id/constraintLayout” app:layout_constraintEnd_toStartOf=”@+id/imageView” app:layout_constraintStart_toStartOf=”@+id/constraintLayout”
    • The heart Count text view aligns the right and bottom sides of its container, essentially the opposite of the ' Japan ' text view.
app:layout_constraintBottom_toBottomOf=”@+id/constraintLayout” app:layout_constraintEnd_toEndOf=”@+id/constraintLayout”
    • Finally, the heart-shaped picture is to the top/bottom of the center-count text view, and the heart-shaped picture view has the right side to the left of the Unity count text view.
app:layout_constraintBottom_toBottomOf=”@+id/text_like_count” app:layout_constraintEnd_toStartOf=”@+id/text_like_count” app:layout_constraintTop_toTopOf=”@+id/text_like_count”

In the layout file, the constrained view is this:

<?xml version= "1.0" encoding= "Utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http// Schemas.android.com/apk/res/android "xmlns:app=" Http://schemas.android.com/apk/res-auto " xmlns:tools= "http://schemas.android.com/tools"android:id="@+id/constraintlayout " <! --  Other attributes -->            ><ImageViewandroid:id="@+id/image_shot"app:layout_constraintbottom_ Tobottomof= "@+id/constraintlayout"app:layout_constraintend_toendof="@+id/ Constraintlayout "app:layout_constraintstart_tostartof=" @+id/constraintlayout "app: Layout_constrainttop_totopof="@+id/constraintlayout" <! --  Other attributes -->                                        /><view  android:layout_ Width  = "match_parent"  android:layout_height< /span>= "50DP"  android:background  = "@color/white"  app:layout_constraintbottom_tobottomof  =         "@+id/constraintlayout"  app:layout_constraintend_toendof  = "@+id/ Constraintlayout " app:layout_constraintstart_tostartof  =" @+id/constraintlayout " <! -- other  attributes  --; /><TextView        Android:id="@+id/text_title"        Android:layout_width="0DP"        Android:layout_height="Wrap_content"        App:layout_constraintbottom_tobottomof="@+id/constraintlayout"        App:layout_constraintend_tostartof="@+id/imageview"        App:layout_constraintstart_tostartof="@+id/constraintlayout"        Tools:text="Japan"<!--  Other attributes -->/><TextViewandroid:id="@+id/text_like_count"app:layout_constraintbottom_ Tobottomof= "@+id/constraintlayout"app:layout_constraintend_toendof="@+id/ Constraintlayout "tools:text=" 1,287 " <! --  Other attributes -->                                /><ImageViewandroid:id="@+id/imageview"android:src="@drawable/ic_ HEART_ACCENT_ACCENT_24DP "app:layout_constraintbottom_tobottomof=" @+id/text_like_count " app:layout_constraintend_tostartof="@+id/text_like_count"app:layout_constrainttop_ Totopof="@+id/text_like_count" <! --  Other attributes -->                                        /></android.support.constraint.ConstraintLayout>

From this simple example, we can see the possibilities offered by Constraintlayout. You can see the use of more constraints in these layout files,Browse Fragment layout, Shot Fragment layout, Comment Item layout and Shot Item Layout.

Why not use Constraintlayout to re-implement some existing layouts?

End

In this article, we quickly browsed through the new Constraintlayout layout, its capabilities and how to use it in our applications to create a more efficient layout. Remember, this layout is still in the alpha phase, and I'm not sure I can use it in a production environment-but be sure to try it when it's still in a wild state! This is just a short post, so be sure to start trying it yourself:)

I'd love to see if/where I used the constraintlayout layout, please give me a push or something to share (or a question) to reply.

Explore Android Constraintlayout layouts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.