Android 从ImageView顶部约束到50%

Android 从ImageView顶部约束到50%,android,android-constraintlayout,Android,Android Constraintlayout,我有这样一个布局: <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView4" android:layout_widt

我有这样一个布局:

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="@+id/textView5"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@tools:sample/backgrounds/scenic[1]" />

        <View
            android:id="@+id/view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="@+id/imageView4"
            app:layout_constraintEnd_toEndOf="@+id/imageView4"
            app:layout_constraintStart_toStartOf="@+id/imageView4"
            app:layout_constraintTop_toTopOf="@+id/imageView4" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Hello"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </android.support.constraint.ConstraintLayout>


基本上,
视图
覆盖在
图像视图
之上。但是,我需要
视图的顶部
图像视图
的中点开始,即距离
图像视图顶部的50%。如何执行此操作?

由于
ImageView
是正方形,因此您也可以在此处使用维度比率属性。移除
视图的顶部约束
使其与底部对齐,并将宽高比设置为
H,2:1
以基于宽度约束高度

<View
    android:id="@+id/view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="H,2:1"
    app:layout_constraintBottom_toBottomOf="@id/imageView4"
    app:layout_constraintEnd_toEndOf="@id/imageView4"
    app:layout_constraintStart_toStartOf="@id/imageView4" />


ConstraintLayout
中的百分比约束仅与视图的父视图相关。看。然而,对于具有
match\u约束的小部件,有“加权链”的概念。看

加权链

链的默认行为是在可用空间中均匀分布元素。如果一个或多个元素使用MATCH_约束,它们将使用可用的空白空间(在它们之间平均分配)。属性layout_ConstraintTorHorizontal_weight和layout_constraintVertical_weight将使用MATCH_CONSTRAINT控制空间在元素之间的分布方式。例如,在包含两个使用MATCH_约束的元素的链上,第一个元素的权重为2,第二个元素的权重为1,第一个元素占用的空间将是第二个元素的两倍

因此,对于这个结果,您的XML应该如下所示。(我已将
视图的背景上色,以便可以看到它



您是否尝试过layout\u Constraint Vertical\u chainStyle=“packed”?不会改变任何东西。它应该做什么?请使用指南
<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">

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/backgrounds/scenic[1]" />

    <Space
        android:id="@+id/space"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toTopOf="@id/view"
        app:layout_constraintEnd_toEndOf="@id/imageView4"
        app:layout_constraintStart_toStartOf="@id/imageView4"
        app:layout_constraintTop_toTopOf="@id/imageView4"
        app:layout_constraintVertical_weight="1" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toBottomOf="@+id/imageView4"
        app:layout_constraintEnd_toEndOf="@+id/imageView4"
        app:layout_constraintStart_toStartOf="@+id/imageView4"
        app:layout_constraintTop_toBottomOf="@+id/space"
        app:layout_constraintVertical_weight="1" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>