Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout ignores margins

I have very simple layout, which can be replaced with one RelativeLayout which always work. However wherever I put margins they are ignored and no, I don't want to use padding and no, chain also doesn't fix the problem. Any idea?

For example margins on date or manufacturer are ignored as well as others.

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:padding="8dp"
    app:cardBackgroundColor="?attr/szykColorSecondary"
    app:cardCornerRadius="8dp"
    app:cardElevation="4dp">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">

        <TextView
            android:id="@+id/date"
            style="?attr/szyk_textMedium"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:gravity="left"
            android:singleLine="true"
            android:text="16.12.2014"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/manufacturer"
            style="?attr/szyk_textBody"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:paddingRight="8dp"
            android:text="Samsung"
            android:textAllCaps="true"
            android:textSize="24sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/model"
            app:layout_constraintTop_toBottomOf="@+id/date" />

        <TextView
            android:id="@+id/model"
            style="?attr/szyk_textBody"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="S6 edge"
            android:textAllCaps="true"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="@+id/manufacturer"
            app:layout_constraintLeft_toRightOf="@+id/manufacturer"
            app:layout_constraintRight_toLeftOf="@+id/delete"
            app:layout_constraintTop_toTopOf="@+id/manufacturer" />

        <ImageView
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_vector_delete"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/serial"
            style="?attr/szyk_textMedium"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="FDSF6D7A8FDAS6F7D89AS"
            android:textSize="12sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/manufacturer" />

        <TextView
            android:id="@+id/restore"
            style="?attr/szyk_textButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:text="@string/restore"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/serial" />

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

Version

compile 'com.android.support.constraint:constraint-layout:1.0.2'

Tools

compileSdkVersion 27
buildToolsVersion '27.0.2'
like image 880
Szymon Klimaszewski Avatar asked Dec 18 '17 09:12

Szymon Klimaszewski


People also ask

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.

What is the difference between ConstraintLayout and LinearLayout?

ConstraintLayout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout). Despite the fact that it's awesome, it fails to serve the purpose with simple UI layouts.

Is ConstraintLayout a ViewGroup?

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 LinearLayout in ConstraintLayout?

Most of what can be done in LinearLayout and RelativeLayout can now be done with a new layout system called ConstraintLayout. It's important to note the class hierarchy of these View Layouts.


1 Answers

Sorry, my bad. The root cause of the issue was style I've been using. It had defined android:layout_margin, which overrides all the more specific margins such as android:layout_marginTop

<item name="android:layout_margin">2dp</item>
like image 88
Szymon Klimaszewski Avatar answered Nov 15 '22 00:11

Szymon Klimaszewski