【问题标题】:Why ConstraintLayout 1.1.0 layout_constraintWidth_percent and layout_constraintDimensionRatio cannot work together?为什么 ConstraintLayout 1.1.0 layout_constraintWidth_percent 和 layout_constraintDimensionRatio 不能一起工作?
【发布时间】:2018-06-23 06:43:52
【问题描述】:

我试图在 ImageView 中同时使用 layout_constraintDimensionRatio 和 layout_constraintWidth_percent 属性,但我发现它们不能一起工作。

我的xml代码是:

1.仅使用 layout_constraintWidth_percent:

<?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:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher"/>
</android.support.constraint.ConstraintLayout>

其对应的UI是: layout_constraintWidth_percent_only.png

2.使用 layout_constraintWidth_percent 和 layout_constraintDimensionRatio:

<?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:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintDimensionRatio="w,2:1"
        android:scaleType="fitXY"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher"/>
</android.support.constraint.ConstraintLayout>

其对应的UI是: layout_constraintWidth_percent_and_layout_constraintDimensionRatio.png

很明显,当我使用 layout_constraintDimensionRatio 时,layout_constraintWidth_percent 不起作用。

我真正想要的是 ImageView 的宽度是其父级的 50%,然后通过将 layout_constraintWidth_percent 和 layout_constraintDimensionRatio 结合使用,ImageView 的高度是其宽度的 50%。

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    android:layout_height="0dp" 设置为匹配约束应该可以帮助您实现所需的结果。

    根据文档:

    如果两个维度都设置为 MATCH_CONSTRAINT (0dp),您也可以使用比率。在这种情况下,系统会设置满足所有约束并保持指定纵横比的最大尺寸。

    <?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:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="fitXY"
            app:layout_constraintWidth_percent="0.5"
            app:layout_constraintDimensionRatio="2:1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher"/>
    
    </android.support.constraint.ConstraintLayout>
    

    编辑 似乎app:layout_constraintWidth_percent 本身并没有为View 提供实际的水平约束,因此以下行为会有所不同(首先设置了right 水平约束,第二个设置了不是):

    <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="fitXY"
            app:layout_constraintDimensionRatio="H,2:1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.5"
            app:srcCompat="@mipmap/ic_launcher" />
    

    <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="fitXY"
            app:layout_constraintDimensionRatio="H,2:1"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.5"
            app:srcCompat="@mipmap/ic_launcher" />
    

    我们根据父元素的百分比来限制宽度,然后通过在app:layout_constraintDimensionRatio="H,2:1" 中使用H 来根据宽度限制高度。在第一种情况下,视图不显示,而在第二种情况下,它确实正确显示。看起来如果你想使用app:layout_constraintWidth_percent,你需要添加两个水平约束,然后你可以根据宽度正确约束高度。

    作为此方法的替代方法,您可以使用 Guideline 设置为父宽度的 50% 来限制您的 ImageView,然后根据比例限制高度:

    <?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:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5" />
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="fitXY"
            app:layout_constraintDimensionRatio="H,2:1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/guideline"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.5"
            app:srcCompat="@mipmap/ic_launcher" />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 您的方法确实有效。还有一个问题,为什么要把 app:layout_constraintDimensionRatio="w,2:1" 改为 app:layout_constraintDimensionRatio="2:1"?
    • 好吧,当我将 app:layout_constraintDimensionRatio="2:1" 更改为 app:layout_constraintDimensionRatio="3:2" 时会发生什么事情?我不知道如何粘贴图片。真丢人。你能帮帮我吗?
    • 我意识到它存在一些问题并进行了更多测试。我更新了我的答案。
    • 你的回答很有道理。
    猜你喜欢
    • 1970-01-01
    • 2020-03-04
    • 2020-12-31
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    相关资源
    最近更新 更多