将16x9图像放置在应用程序窗口的上方(约束布局)(Android Studio)

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

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

enter image description here

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

我尝试使用约束布局,但是根据屏幕宽度,它会更改大小和比例。我还将要在其上添加一些文本和一幅图片。

android android-studio imageview
1个回答
0
投票

您可以使用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>
© www.soinside.com 2019 - 2024. All rights reserved.