ConstraintLayout中心布局

时间:2018-12-19 09:59:29

标签: android android-layout android-constraintlayout

我有以下项目

enter image description here

并且我想包含在另一个这样的布局中

enter image description here

问题是底部的两个图像没有居中

这是我的代码,我需要知道如何将它们居中

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/layout"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:background="@drawable/xml_button_white_bg">

    <ImageView
            android:id="@+id/checkImageView"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_alignParentEnd="true"
            android:src="@drawable/ic_ckeck"
            android:tint="@color/colorPrimary"
            android:visibility="visible"
            android:layout_marginEnd="4dp"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginTop="4dp"
            app:layout_constraintTop_toTopOf="parent"/>

    <ImageView
            android:id="@+id/iconImageView"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_centerHorizontal="true"
            android:src="@drawable/ic_placeholder_squre"
            app:layout_constraintBottom_toTopOf="@+id/titleTextView"
            app:layout_constraintTop_toBottomOf="@+id/checkImageView"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"/>

    <TextView
            android:id="@+id/titleTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/iconImageView"
            android:ellipsize="end"
            android:gravity="center"
            android:lines="2"
            android:text="@string/text"
            android:textColor="@android:color/black"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="4dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="4dp"
            android:layout_marginEnd="4dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是包含所有项目的布局代码

<androidx.constraintlayout.widget.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:background="@android:color/transparent"
            android:layout_height="wrap_content">

        <include layout="@layout/item_square_bool"
                 android:id="@+id/inAppCheck"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginStart="8dp"
                 app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintEnd_toStartOf="@+id/voipCheck"></include>

        <include layout="@layout/item_square_bool"
                 android:id="@+id/voipCheck"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginStart="8dp"
                 android:layout_marginEnd="8dp"
                 app:layout_constraintEnd_toStartOf="@+id/anonymousCheck"
                 app:layout_constraintStart_toEndOf="@+id/inAppCheck"></include>

        <include layout="@layout/item_square_bool"
                 android:id="@+id/anonymousCheck"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginEnd="8dp"
                 app:layout_constraintEnd_toEndOf="parent"
                 app:layout_constraintStart_toEndOf="@+id/voipCheck"></include>

        <include layout="@layout/item_square_bool"
                 android:id="@+id/autoPostCheck"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="8dp"
                 android:layout_marginStart="8dp"
                 app:layout_constraintTop_toBottomOf="@+id/voipCheck"
                 app:layout_constraintStart_toStartOf="parent"></include>

        <include layout="@layout/item_square_bool"
                 android:id="@+id/doNotDisturbCheck"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="8dp"
                 android:layout_marginEnd="8dp"
                 app:layout_constraintTop_toBottomOf="@+id/voipCheck"
                 app:layout_constraintEnd_toEndOf="parent"></include>



    </androidx.constraintlayout.widget.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

您需要使用Chains

  • 链在单轴(水平或垂直)上提供类似组的行为。另一根轴可以独立约束。

有关更多信息,请检查

示例代码

<androidx.constraintlayout.widget.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="wrap_content"
    android:background="@android:color/transparent">

    <include
        android:id="@+id/inAppCheck"
        layout="@layout/item_square_bool"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        app:layout_constraintEnd_toStartOf="@+id/voipCheck"
        app:layout_constraintStart_toStartOf="parent" />

    <include
        android:id="@+id/voipCheck"
        layout="@layout/item_square_bool"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toStartOf="@+id/anonymousCheck"
        app:layout_constraintStart_toEndOf="@+id/inAppCheck" />

    <include
        android:id="@+id/anonymousCheck"
        layout="@layout/item_square_bool"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/voipCheck" />

    <include
        android:id="@+id/autoPostCheck"
        layout="@layout/item_square_bool"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toStartOf="@+id/doNotDisturbCheck"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/voipCheck" />

    <include
        android:id="@+id/doNotDisturbCheck"
        layout="@layout/item_square_bool"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/autoPostCheck"
        app:layout_constraintTop_toBottomOf="@+id/voipCheck" />


</androidx.constraintlayout.widget.ConstraintLayout>

输出

enter image description here