android - 在 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_height
为ImageView
固定值,那么我可以让它在一个显示宽度上正常工作,但不能在不同的宽度上工作。如果我将其设置为layout_height
,wrap_content
那么尽管设置了图像,但图像垂直居中而不是位于显示器的顶部,layout_constraintTop_toTopOf="parent"
除非我设置scaleType="fitStart"
了将图像移动到显示器顶部的设置。
但是,scaleType="fitStart"
在屏幕底部和底部TextView
之间的空间中垂直居中显示 。不在尽管设置ImageView
的正下方ImageView
layout_constraintTop_toBottomOf="@+id/imageLogo"
ImageView
当以全屏宽度显示时,如何让图像自动缩放到图像的高度,并允许其TextView
立即位于下方?
解决方案
我已经解决了这个问题......它ImageView
被显示为一个正方形图像,高度和宽度都设置为显示器的宽度和TextView
正方形的下方。
将其添加到以ImageView
原始纵横比显示图像,一切都按预期显示:)
机器人:adjustViewBounds =“真”
推荐阅读
- node.js - 如何在 Graphiql 工具中使用查询参数
- python - 应用程序中的多语言支持
- php - 更改联系人 7 表格中的日期格式
- antlr4 - 如何让antlr4解析部分正确语法而不是抛出错误节点?
- java - 使用 JavaFX 12 在 Swing 中创建 JFXPanel 时如何修复异常
- ios - 使用 CoreBluetooth CBL2CAPChannel
- excel - x = x + y 公式在 Excel VBA 中返回 0
- node.js - puppeteer headless chrome 是否可以在 Azure 应用服务的 Windows 容器中工作?
- go - 观察循环内的无效值
- php - 插入表格在 foreach Laravel 5.8 中不起作用