如何使用ConstraintLayout在DimensionRatio上设置maxHeight? [英] How to set maxHeight over DimensionRatio with ConstraintLayout?

查看:1050
本文介绍了如何使用ConstraintLayout在DimensionRatio上设置maxHeight?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以1220:1000的尺寸比例和最大300dp的最大高度显示以父级为中心的图像(即使在大屏幕上也要保持较小的图像)

I'm trying to display an image centered in parent with a dimension ratio of 1220:1000 AND a maximum height of 300dp (to keep the image small even with large screen)

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1220:1000"
        app:layout_constraintHeight_max="200dp"  ==> This line break the ratio (the image is not displayed)
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

app:layout_constraintHeight_max属性打破比率. 有办法吗?

The app:layout_constraintHeight_max property break the ratio. Is there is a way to do it?

推荐答案

如果要强制执行max_height并同时保持尺寸比例,则需要根据高度限制宽度.您可以通过在比率中添加W来实现这一目标:

If you want to enforce max_height and keep the dimension ratio at the same time, you need to constrain the width based on height. You can achieve that by adding W to your ratio:

app:layout_constraintDimensionRatio="W,1220:1000"

这将首先限制高度,然后相应地设置宽度以满足比率.

This will constrain the height first and then set the width accordingly to satisfy the ratio.

有关此尺寸比例如何工作的更多信息,请参见文档.

More info on how this dimension ratio works can be found in the documentation.

这篇关于如何使用ConstraintLayout在DimensionRatio上设置maxHeight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆