Based on the fact that Constraint Layout was created to avoid container nesting (one of the reasons), how then to avoid nesting in the case when you need to divide the layout into 2 colors?

Like this

enter image description here

Here is the XML

 <?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" android:orientation="vertical"> <android.support.constraint.Guideline android:id="@+id/guideline7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.5" tools:layout_editor_absoluteX="192dp" tools:layout_editor_absoluteY="0dp"/> <android.support.constraint.ConstraintLayout android:layout_width="0dp" android:layout_height="0dp" android:background="#000000" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/guideline7" app:layout_constraintTop_toTopOf="parent"> </android.support.constraint.ConstraintLayout> <android.support.constraint.ConstraintLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_marginBottom="0dp" android:layout_marginLeft="0dp" android:layout_marginRight="0dp" android:layout_marginTop="0dp" android:background="#FFFFFF" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="@+id/guideline7" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0"> </android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout> 

But it still turns out that I need to do the nesting

Or not?

  • In your example, one level of nesting can not be less. What is the problem then? - eugeneek
  • @eugeneek The problem is that according to Google's idea, ConstrainLayout implies only the root container ( ConstraintLayout itself), without any nested containers, only widgets. In the question it is easy to notice 2 nested containers, also ConstrainLayout - pavlofff
  • Well, OK. Replace nested ConstrainLayout with just View . The result will be exactly the same. - eugeneek
  • @eugeneek yes, but I want to keep the ConstrainLayout features ... If I just make a view, it won't be Constrain - Aleksey Timoshchenko
  • If you want to place something else in them, then place them in a root framework that has exactly the same capabilities. In response, you were shown an example. If there are difficulties with a particular layout, then it was worth adding it to the question. - eugeneek

1 answer 1

There is no need to do nesting, here is an example based on your markup:

 <?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" android:orientation="vertical"> <View android:layout_width="0dp" android:layout_height="0dp" android:background="#000000" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@+id/guideline7" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:src="@mipmap/ic_launcher_round" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toStartOf="@+id/guideline7" app:layout_constraintStart_toStartOf="parent" tools:layout_editor_absoluteY="0dp" app:layout_constraintHorizontal_bias="0.33" android:id="@+id/imageView" /> <android.support.constraint.Guideline android:id="@+id/guideline7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" tools:layout_editor_absoluteX="192dp" tools:layout_editor_absoluteY="0dp" app:layout_constraintGuide_begin="192dp" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_marginBottom="0dp" android:layout_marginLeft="0dp" android:layout_marginRight="0dp" android:layout_marginTop="0dp" android:background="#FFFFFF" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="@+id/guideline7" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <SeekBar android:id="@+id/seekBar" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:progress="45" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/guideline7" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.8" /> </android.support.constraint.ConstraintLayout> 

Result:

result

  • Now I understand, I just did not know about these app parameters: layout_constraintEnd_toStartOf = "@ + id / guideline7" app: layout_constraintStart_toStartOf = "parent". Thank you - Aleksey Timoshchenko
  • Yes, I know it thanks - Aleksey Timoshchenko