使用 ConstraintLayout 将图像定位到屏幕顶部

Positionning a image to the top of screen with ContraintLayout

我希望使用 ConstraintLayout 将简单图像与屏幕顶部对齐。我刚试过:

  <ImageView
    android:id="@+id/imagecookie"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/before_cookie"
    app:layout_constraintTop_toBottomOf="parent" />

图片定位在屏幕中间。 有什么帮助吗? 谢谢

您正在将 ImageView 的顶部设置为父级 (ConstraintLayout) 的 bottom,您需要将其设置为顶部

编辑 整个布局的示例

<?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"
    android:background="@color/colorPrimary"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

结果

感谢您的宝贵时间和帮助,但它仍未位于屏幕顶部。 我将我的图片放在屏幕顶部(和中间水平):

 <ImageView
     android:id="@+id/imagecookie"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/suzuki"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toTopOf="parent" />