Ограничить до 50% от верхней части ImageView - PullRequest
0 голосов
/ 29 октября 2018

У меня есть такой макет:

    <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>

По существу, View накладывается поверх ImageView. Однако мне нужно, чтобы вершина View начиналась со средней точки ImageView, то есть 50% от вершины ImageView. Как мне это сделать?

Ответы [ 2 ]

0 голосов
/ 29 октября 2018

Процентные ограничения в ConstraintLayout работают только по отношению к родителю представления. См. документацию . Однако существует концепция «взвешенных цепочек» для виджетов, которые имеют match_constraints. Смотри здесь .

Весовые цепи

Поведение по умолчанию в цепочке - это равномерное распределение элементов в доступном пространстве. Если один или несколько элементов используют MATCH_CONSTRAINT, они будут использовать доступное пустое пространство (поровну разделенное между собой). Атрибут layout_constraintHorizont_weight и layout_constraintVertical_weight будет управлять распределением пространства между элементами с помощью MATCH_CONSTRAINT. Например, в цепочке, содержащей два элемента, использующих MATCH_CONSTRAINT, причем первый элемент использует вес 2, а второй - 1, пространство, занимаемое первым элементом, будет в два раза больше, чем у второго элемента.

Итак, ваш XML должен выглядеть примерно так, как показано ниже. (Я покрасил фон View, чтобы его было видно.

enter image description here

<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>
0 голосов
/ 29 октября 2018

Поскольку ImageView является квадратом, вы также можете использовать здесь атрибут размерного соотношения. Удалите верхнее ограничение View, чтобы выровнять его по низу, и установите соотношение ширины и высоты на 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" />
...