繁体   English   中英

在RelativeLayout中顶部对齐ImageView和TextView

[英]Top aligning ImageView and TextView in RelativeLayout

我在显示屏顶部有公司徽标图像,在此下方,我希望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>

如果将ImageViewlayout_height设置为固定值,则可以使其在一个显示宽度上正常工作,但不能在其他宽度上工作。 如果我将layout_height设置为wrap_content则尽管设置了layout_constraintTop_toTopOf="parent" ,但图像仍垂直居中而不是位于显示顶部,除非我设置了scaleType="fitStart"将图像移动到显示顶部。

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

当全屏显示时,如何使ImageView自动缩放到图像的高度,并以允许TextView直接位于其下方的方式显示?

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

将其添加到ImageView会以其原始纵横比显示图像,并且所有内容均按预期显示:)

机器人:adjustViewBounds = “真”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM