How to include a constraint layout in another constraint pattern and set a constraint between

I am using constraintLyout v 1.0.1.

I would like to include in my xml an optional ConstraintLayout object corresponding to the part of my global layout (which is ConstraintLayout itself). I laid out the layout in two xmls to use this part elsewhere

I tried this, but I have no control over where to place the layout of the auxiliary constraint in the parent. I wonder if I need to put everything in one XML file or if it is a solution for using separate files.

tmp_1.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="LABEL1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="16dp" /> <TextView android:id="@+id/label_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="LABEL2" app:layout_constraintStart_toStartOf="@id/label" app:layout_constraintEnd_toEndOf="@id/label" app:layout_constraintTop_toBottomOf="@id/label" android:layout_marginTop="16dp" /> <include layout="@layout/tmp_2" /> </android.support.constraint.ConstraintLayout> 

tmp_2.xml

 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/view_80" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="80th element" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="10dp" android:layout_marginStart="12dp" /> </android.support.constraint.ConstraintLayout> 

The result is the actual result.

But I want it to be the Expected Result

I tried this, but it does not work.

 <include app:layout_constraintTop_toBottomOf="@id/label_2" layout="@layout/tmp_2" /> 

I would be happy to receive your decisions,

thanks

+8
android android-layout xml include android-constraintlayout
source share
2 answers

Actually found a solution. Android Studio does not autocomplete the constraintLayout parameters in the include tag, but they do affect it as long as you specify that they include size.

 <include layout="@layout/tmp_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/label_2" /> 
+24
source share

You can avoid ConstraintLayout restrictions in the include element. I just <include/> as it is.

MainActivity Layout File:

 <?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:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/toolbarLayout" layout="@layout/layout_toolbar" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="CONTENTS" app:layout_constraintBottom_toBottomOf="@+id/footerLayout" app:layout_constraintEnd_toEndOf="@+id/footerLayout" app:layout_constraintStart_toStartOf="@+id/footerLayout" app:layout_constraintTop_toTopOf="@+id/footerLayout" /> <include android:id="@+id/footerLayout" layout="@layout/layout_footer" /> </android.support.constraint.ConstraintLayout> 

ToolBar Layout File:

 <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:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:minHeight="?attr/actionBarSize" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> <TextView android:id="@+id/toolbarTitleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/hidden" android:textColor="@android:color/white" tools:layout_editor_absoluteX="192dp" tools:layout_editor_absoluteY="19dp" /> </android.support.v7.widget.Toolbar> </android.support.constraint.ConstraintLayout> 
0
source share

All Articles