Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout margins

I have a simple layout.

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorDarkGray">


    <RelativeLayout
        android:id="@+id/container3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:alpha="0.7"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Another text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Some text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

In design view, it looks so looks so As you can see there is no left and right margins. Top and bottom margins work well. If I remove left or right margin my relative layout moves beyond the screen a bit. Guess I can do it without a relative layout at all, BUT I'm interested why that happened.

like image 231
TooLazy Avatar asked Nov 07 '17 10:11

TooLazy


People also ask

What are ConstraintLayout constraints?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).

Can we use linear layout in ConstraintLayout?

You can create linear layouts now with ConstraintLayout by constraining the sides of each element with each other. The quick way of creating these layouts is to select all the views together and right click to center horizontally or vertically.

Which is better RelativeLayout or ConstraintLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI.


2 Answers

You should avoid using nested views inside ConstraintLayout. The reasons are:

  • ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups) as it's stated in Build a Responsive UI with ConstraintLayout

  • Traditional nested layout hierarchy negatively impacts on performance as it's stated in Understanding the performance benefits of ConstraintLayout

The result layout source should look like:

<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="wrap_content"
    android:background="@color/colorDarkGray">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:alpha="0.7"
        android:fontFamily="sans-serif"
        android:letterSpacing="0.03"
        android:text="Another text"
        android:textColor="@color/colorWhite"
        android:textSize="11sp"
        android:textStyle="normal"
        android:id="@+id/textView"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/textView2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="end"
        android:fontFamily="sans-serif"
        android:letterSpacing="0.03"
        android:text="Some text"
        android:textColor="@color/colorWhite"
        android:textSize="11sp"
        android:textStyle="normal"
        android:id="@+id/textView2"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

Result view

You can learn how to build layouts with ConstraintLayouts by following steps in Google's Codelab.

like image 58
Eugene Brusov Avatar answered Oct 19 '22 13:10

Eugene Brusov


Try this change your RelativeLayout width to android:layout_width="0dp"

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorDarkGray">


    <RelativeLayout
        android:id="@+id/container3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:alpha="0.7"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Another text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Some text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>
like image 26
Ratilal Chopda Avatar answered Oct 19 '22 13:10

Ratilal Chopda