答案 0 :(得分:8)
您需要创建一个连接在其父容器所有面上的小部件链(您可以将此父容器的尺寸设置为您想要/需要的任何尺寸)。每个小部件都应具有app:layout_constraintDimensionRatio="1:1"
约束以确保方形图案。例如:
<?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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/frame2"
app:layout_constraintTop_toTopOf="parent"
/>
<FrameLayout
android:id="@+id/frame2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/frame1"
app:layout_constraintRight_toLeftOf="@+id/frame3"
app:layout_constraintTop_toTopOf="parent"
/>
<FrameLayout
android:id="@+id/frame3"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/frame2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
答案 1 :(得分:0)
使用带有百分比的指南,如下所示:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/guideline"
app:layout_constraintGuide_begin="316dp"
android:orientation="horizontal" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.502" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/guideline2"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.2" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toLeftOf="@+id/guideline2"
app:layout_constraintRight_toLeftOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.502" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/guideline3"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.4" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toLeftOf="@+id/guideline3"
app:layout_constraintRight_toLeftOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.502" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/guideline4"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toLeftOf="@+id/guideline4"
app:layout_constraintRight_toLeftOf="@+id/guideline5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.503" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/guideline5"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.8" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toLeftOf="@+id/guideline5"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
要添加指南,请右键单击ConstraintLayout / Add Vertical / Horisontal Guideline ... 希望它能帮到你
干杯
答案 2 :(得分:-1)
您只需要将视图的约束添加到第一个视图。在顶部和底部。
<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">
<android.support.v7.widget.CardView
android:id="@+id/uno"
android:layout_width="0dp"
android:layout_height="0dp"
app:cardBackgroundColor="@color/colorPrimary"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/dos"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="8dp"/>
<android.support.v7.widget.CardView
android:id="@+id/dos"
android:layout_width="0dp"
android:layout_height="0dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/tres"
app:layout_constraintStart_toEndOf="@+id/uno"
app:layout_constraintTop_toTopOf="@id/uno"
app:layout_constraintBottom_toBottomOf="@id/uno"/>
<android.support.v7.widget.CardView
android:id="@+id/tres"
android:layout_width="0dp"
android:layout_height="0dp"
app:cardBackgroundColor="@color/colorAccent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/dos"
app:layout_constraintTop_toTopOf="@id/uno"
app:layout_constraintBottom_toBottomOf="@id/uno" />