无法将图像视图与左边缘对齐

问题描述 投票:0回答:1

我想将下部视图与顶部视图的左边缘对齐

在此输入图片描述

但他们就这样走了:

在此输入图片描述

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginStart="56dp"
        android:layout_marginTop="24dp"
        android:scaleType="centerCrop"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_code" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/ic_code"
        tools:layout_editor_absoluteX="56dp"
        tools:layout_editor_absoluteY="174dp" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/ic_code"
        tools:layout_editor_absoluteX="56dp"
        tools:layout_editor_absoluteY="333dp" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/ic_code"
        tools:layout_editor_absoluteX="56dp"
        tools:layout_editor_absoluteY="501dp" />


</androidx.constraintlayout.widget.ConstraintLayout>

我已经做过很多次了,但它一直这样。

android android-constraintlayout
1个回答
1
投票

您发布的约束布局不准确且不正确。所有观点都不受彼此约束。

我们是这样做的:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:paddingStart="56dp"> // this aligns all the child views to 56 dp from the Start

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginTop="24dp"
        android:scaleType="centerCrop"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_code" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginTop="30dp" // add the required dp for gap between 2nd and 1st image
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/ic_code"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageButton" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginTop="30dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/ic_code"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageButton1" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginTop="30dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/ic_code"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageButton2" />

</androidx.constraintlayout.widget.ConstraintLayout>

请根据您的需求约束您的子视图,约束布局提供了多种约束选项,建议您参考Android的文档:https://developer.android.com/develop/ui/views/layout /约束布局

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.