首页 > 解决方案 > 不可能使用 android 中的约束布局将图像放在顶部

问题描述

我试图将图像标题放在约束布局的顶部但是当我在真实设备中运行应用程序时它是错误的并且标题不在顶部:

在此处输入图像描述


<?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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/header"
        android:layout_width="0dp"
        android:layout_height="207dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_header" />
    
</androidx.constraintlayout.widget.ConstraintLayout>

下面我们可以看到它在真实设备中的外观:

在此处输入图像描述

操作栏和标题之间有一个空格。我试图删除操作栏但也不起作用。提前致谢。

这是我的 ic_header 内容:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="360dp"
    android:height="180dp"
    android:viewportWidth="360"
    android:viewportHeight="180">
  <path
      android:pathData="M0,0h360v180h-360z"
      android:fillColor="#fbc711"/>
</vector>

标签: androidandroid-constraintlayoutandroid-designer

解决方案


I think the issue is with the src drawable and the ImageView both having a fixed height.

Try using android:scaleType="centerCrop" in your ImageView xml, so that the source image expands to cover the empty area.


推荐阅读