简体   繁体   中英

ConstraintLayout - ImageView problem with bottom margin

I am trying to design a simple puzzle game where I use the ConstraintLayout and the goal is to have a 4x4 grid of ImageViews with the same size. The source image is the same for all ImageViews so all elements will have the same size.

The problem is that I set the bottom margin of all elements to 0dp but the alignment is different and "the grid" of images is not consistent. The second ImageView looks like there is a small bottom margin even if the margin is set to 0dp .

I am using guidelines to divide content and create "the grid". Does anybody know what can cause this issue?

不同的下边距

The activity source code:

<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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/imageView17"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/img0" />

<ImageView
    android:id="@+id/imageView22"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toTopOf="@+id/imageView17"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/img0" />

<ImageView
    android:id="@+id/imageView18"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/guideline2"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:srcCompat="@drawable/img0" />

<ImageView
    android:id="@+id/imageView23"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toTopOf="@+id/imageView18"
    app:layout_constraintEnd_toStartOf="@+id/guideline2"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:srcCompat="@drawable/img0" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.25" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.75" />

I copy pasted your code in my Android Studio. Your code worked fine in my design view (See image attached below). I suggest you the following steps

  1. Force Refresh your Design View if it doesn't work then
  2. try restarting Android Studio
  3. If problem still persists try updating android studio or contact support.

测试

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM