簡體   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