图像视图在约束布局中移至左侧

时间:2020-05-25 15:40:16

标签: android android-studio user-interface

我在屏幕的四个边缘使用ImageView创建了一个布局 Layout Image 但是当我运行该应用程序时,ImageView会向左移 Screenshot of activity

我无法找出问题所在

我也提供xml布局文件。我想将图像视图设置在屏幕中央。我只是提供布局的“图像视图”部分。

XML布局

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/semiTransparent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:contentDescription="@string/image_of_the_song"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.3"
        app:srcCompat="@android:drawable/divider_horizontal_bright" />


</androidx.constraintlayout.widget.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

您的问题是无法满足约束条件,您在设置图像视图的宽度和高度的同时还试图水平约束它。

更改此

<ImageView
    android:id="@+id/imageView"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:contentDescription="@string/image_of_the_song"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.3"
    app:srcCompat="@android:drawable/divider_horizontal_bright" />

对此

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:adjustViewBounds="true"
    android:contentDescription="@string/image_of_the_song"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.3"
    app:srcCompat="@android:drawable/divider_horizontal_bright" />