首页 > 解决方案 > 在 RelativeLayout 中顶部对齐 ImageView 和 TextView

问题描述

我在显示器顶部有一个公司徽标图像,在它的正下方我想要TextView显示当前用户。但是它必须显示在不同宽度的显示器上,这些显示器总是手机。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <ImageView
        android:id="@+id/imageLogo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitStart"
        android:contentDescription="@string/lets_delight_logo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />

    <TextView
        android:id="@+id/user"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_marginLeft="35dp"
        android:gravity="left"
        android:text="Not logged on..."
        app:layout_constraintTop_toBottomOf="@+id/imageLogo" />

</android.support.constraint.ConstraintLayout>

如果我将 的 设置layout_heightImageView固定值,那么我可以让它在一个显示宽度上正常工作,但不能在不同的宽度上工作。如果我将其设置为layout_heightwrap_content那么尽管设置了图像,但图像垂直居中而不是位于显示器的顶部,layout_constraintTop_toTopOf="parent"除非我设置scaleType="fitStart"了将图像移动到显示器顶部的设置。

但是,scaleType="fitStart"在屏幕底部和底部TextView之间的空间中垂直居中显示 。不在尽管设置ImageView的正下方ImageViewlayout_constraintTop_toBottomOf="@+id/imageLogo"

ImageView当以全屏宽度显示时,如何让图像自动缩放到图像的高度,并允许其TextView立即位于下方?

标签: androidandroid-layoutandroid-relativelayout

解决方案


我已经解决了这个问题......它ImageView被显示为一个正方形图像,高度和宽度都设置为显示器的宽度和TextView正方形的下方。

将其添加到以ImageView原始纵横比显示图像,一切都按预期显示:)

机器人:adjustViewBounds =“真”


推荐阅读