如何在ConstraintLayout Android Studio中创建重叠链

时间:2018-12-20 22:52:46

标签: android android-studio android-constraintlayout overlap

我想创建一个重叠链,该链可以比其他元素保留所需的余量,并且它们可能具有更多彼此相邻的元素。像这样:

1 个答案:

答案 0 :(得分:1)

可以使用Guidelines来实现。尝试这样的事情:

<?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="match_parent">

  <View
      android:id="@+id/view_1"
      android:layout_width="200dp"
      android:layout_height="200dp"
      android:background="#f00"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

  <android.support.constraint.Guideline
      android:id="@+id/guideline_1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintGuide_percent="0.26" />

  <View
      android:id="@+id/view_2"
      android:layout_width="200dp"
      android:layout_height="200dp"
      android:background="#0f0"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintStart_toEndOf="@id/guideline_1"
      app:layout_constraintTop_toTopOf="parent" />

  <android.support.constraint.Guideline
      android:id="@+id/guideline_2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintGuide_percent="0.52" />

  <View
      android:id="@+id/view_3"
      android:layout_width="200dp"
      android:layout_height="200dp"
      android:background="#00f"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintStart_toEndOf="@id/guideline_2"
      app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>