将 16x9 图像放置在应用 window(约束布局)(android 工作室)的上侧

Positioning a 16x9 image in the upper side of the app window (constraints layout) (android studio)

我有一些 16x9 的图像,我想像使用来自 facebook 的封面图像一样使用它们。 像这样:

想法是我希望它固定在上部而不改变它的比例

我尝试使用约束布局进行此操作,但它会根据屏幕宽度更改大小和比例。 我还想在上面添加一些文字和一张图片。

您可以使用 app:layout_constraintDimensionRatio="H,16:9" 这样高度会根据宽度进行调整,因此当屏幕宽度从设备更改为其他设备时,高度将始终为宽度的 9 比

<?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">

<ImageView
    android:id="@+id/cover"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#0000FF"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintDimensionRatio="H,16:9" /> 
<!-- here you can add other views below -->
<TextView
    app:layout_constraintTop_toBottomOf="@id/cover"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginTop="16dp"
    android:text="Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>