简体   繁体   中英

Top aligning ImageView and TextView in RelativeLayout

I have a company logo image at the top of the display and immediately underneath this I want a TextView to display the current user. However it must display on different width displays which are always mobile phones.

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

If I set the layout_height of the ImageView to a fixed value then I can get it to work right on one display width but not a different width. If I set the layout_height to wrap_content then the image is vertically centered instead of being at the top of the display despite setting layout_constraintTop_toTopOf="parent" unless I set scaleType="fitStart" which moves the image to the top of the display.

However, with scaleType="fitStart" the TextView is displayed vertically centered in the space between the bottom of the ImageView and the bottom of the screen. Not immediately below the ImageView despite setting layout_constraintTop_toBottomOf="@+id/imageLogo"

How can I get the ImageView to automatically scale to the height of the image when displayed at full screen width and in a way that allows the TextView to locate immediately underneath?

I've solved the problem... The ImageView was being displayed as a square image with both the height and width being set to the width of the display and the TextView under the square.

Adding this to the ImageView displays the image in its native aspect ratio and everything displays as expected :)

android:adjustViewBounds="true"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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