How to make a relative position to sees in ConstraintLayout



  • ConstraintLayout Relative positioning of the encumbered items is required.

    For example, a picture up to one third of the screen height and down there are two buttons, each on half the screen, so that the markings are equally visible on all devices.



  • We'll retrieve this marking in the visual editor's mode, because it'll make it very easier for us to live in this case because of the long names of the attributes that use this container.

    You can read it. введите сюда описание изображения

    UPD: C Android Studio 2.2 Preview3 The backbone lines can be put out of the instrument panel up the editor:

    введите сюда описание изображения

    The automatic connection of the viewing (upstream with the blue ends, in the left upper corner, shall be cut off), as this function does not always understand well what a person wants.

    The default support line shall be positioned at relative coordinates (resistance in the position dpI'm telling you what the picture of the shooter is in that line. But we need a relative positioning as a percentage, so we throw a mouse on this arrow in the circus and it takes a view of the interest--- moving for this circle in the right positions: vertically set at 50 per cent, and horizontally installed at 34 per cent (three screens), then throwing three viewpoints we need (three screens)ImageView and two Button😞

    введите сюда описание изображения

    Now we attach to these lines our viewpoints, we click on the view and connect the circles on their edges to the connecting lines: the button - the left button with the left edge of the screen and the lower point of the low screen, right circumference with the vertical support line, right button in the mirror only.UPD From 1.0. введите сюда описание изображения

    The horizontal size of the buttons is defined as the maximum distance (a tax) match_parentin a square to the right cheek on the horizontal line until it appears as "pila" - введите сюда описание изображения (triangles - wrap_content, cut - fixed size. Vertical leaves as it is. wrap_content) We're putting on the margins, the number of pictograms of the size of the view (click here 16dp and 8dp).

    All the buttons.

    Now the lower edge (low) at ImageView connect to the horizontal support line. Other edges shall be connected to the edges of the screen:

    введите сюда описание изображения

    We'll put the margins at ImageView 0, size wrap_content:

    введите сюда описание изображения

    That's it. The description was very long, but in reality it took less than a minute. The results are as follows:

    введите сюда описание изображения

    This marking will retain its relative view on any screen size with any density.

    It should also be noted that ConstraintLayout preferred RelativeLayoutas much better optimized and calculated faster, and more positioning opportunities. For example, this marking would have to use a number of "classical" containers.

    If the view is required to indicate additional properties, we shall click on the pictogram of the dispersed arrows in the top right of the editor ' s corner, all attributes available to view:

    введите сюда описание изображения

    Google. https://codelabs.developers.google.com/codelabs/constraint-layout/index.html#0 A new instrument, which can be seen, can be learned from the main points of work.

    in xml-vis all this is as follows:

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/constraintLayout">
    
    &lt;android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        android:orientation="vertical"
        app:relativePercent="50" /&gt;
    
    &lt;android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        android:orientation="horizontal"
        app:relativePercent="34" /&gt;
    
    &lt;Button
        android:text="Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        app:layout_constraintLeft_toLeftOf="@+id/guideline"
        android:layout_marginLeft="8dp"
        app:layout_constraintRight_toRightOf="@+id/constraintLayout"
        android:layout_marginRight="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
        android:layout_marginBottom="16dp" /&gt;
    
    &lt;Button
        android:text="Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
        android:layout_marginLeft="16dp"
        app:layout_constraintRight_toLeftOf="@+id/guideline"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
        android:layout_marginBottom="16dp" /&gt;
    
    &lt;ImageView
        android:src="@drawable/bk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:adjustViewBounds="true"
        app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        app:layout_constraintRight_toRightOf="@+id/constraintLayout"
        app:layout_constraintBottom_toTopOf="@+id/guideline2" /&gt;
    

    </android.support.constraint.ConstraintLayout>




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2