[英]Android ConstraintLayout aspect ratio
我有一个带有图像和文本视图的 ContrainstLayout 页面。 对于图像,我希望 W:H 比率为 1:1。 但出于某种原因,图像总是有开始和结束边距(即使我将边距设置为 0)
下面是包含布局的父页面(在“ViewPager”的位置):
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="H,1:1"/>
<TextView
android:id="@+id/tvCaption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/guideText"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/imgView"/>
</android.support.constraint.ConstraintLayout
您指定的比例将为您提供一个比例为 1:1 的ImageView 。 您的图像保持其纵横比并位于ImageView 内。 我猜图像比宽高一点,所以这就是为什么你会看到侧面的条。 要完全填充 1:1 ImageView ,您需要以某种方式裁剪顶部和/或底部的图像
为ImageView尝试以下操作。 我想你会发现它填满了屏幕的宽度并裁剪了图像的高度。 有关android:scaleType="centerCrop"
工作原理的详细信息,请参阅scaleType 。 Android ImageView ScaleType:可视化指南也派上用场。
<ImageView
android:id="@+id/imgView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/mypngimage"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="H,1:1"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.