Подтвердить что ты не робот

ConstraintLayout layout_constraintDimensionRatio не работает

Я использовал constraintLayout и layout_constraintDimensionRatio = "1:1" (ширина равна warp_content, высота равна 0dp (match_constraint))

В результате я ожидал, что ширина и высота будут 1:1, но не будут работать.

что случилось??

Я приложил код и скриншот.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center"
        android:text="Hello World!11"
        app:layout_constraintDimensionRatio="1:1" />

</android.support.constraint.ConstraintLayout>

Скриншот

Я цитирую сайт разработчика Android о Constraintlayout. https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints

"Соотношение :: Вы также можете определить одно измерение виджета как отношение другого. Чтобы сделать это, вам нужно, чтобы хотя бы одно ограниченное измерение было установлено равным 0dp (т.е. MATCH_CONSTRAINT), и установите атрибут layout_constraintDimentionRatio к заданному соотношению. Например:

     <Button android:layout_width="wrap_content"
               android:layout_height="0dp"
               app:layout_constraintDimensionRatio="1:1" />

установит высоту кнопки равной ее ширине. "

→ но я не работал.

4b9b3361

Ответ 1

Вы забыли добавить свои ограничения

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

    <TextView
        android:id="@+id/t1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center"
        android:text="Hello World!11"
        app:layout_constraintDimensionRatio="1" />

</android.support.constraint.ConstraintLayout>

0dp применяется только к дочерним представлениям ConstraintLayout. Любое представление должно применять правила атрибута его родителя.

Ответ 2

Начиная с версии 1.1.0 это изменилось.

Теперь вы можете определить:

app:layout_constraintDimensionRatio="1:1"
app:layout_constraintDimensionRatio="W,1:1"
app:layout_constraintDimensionRatio="H,1:1"

Проверьте ссылку ниже, чтобы найти всю документацию относительно DimensionConstraints:

Ссылка на документы

Ответ 3

В моем случае у меня есть проблема, как будто я должен заполнить свой макет внутри контейнера форматом бумаги формата А4.

проблема

Я получаю страницы резюме формата A4 в виде изображений из серверной части, поэтому мне нужно добавить эти изображения в Viewpager, в котором я использую ImageView для отображения этих изображений.

Решение

Я просмотрел документ макета Constraint, в котором есть раздел Ratio. Так что я могу использовать layout_constraintDimensionRatio для решения моей проблемы.

enter image description here

Таким образом, мой xml, который используется для отображения всего макета, выглядит следующим образом, в моем случае я использовал app:layout_constraintDimensionRatio="1:1.27" как с: высотой, но фактическое соотношение - app:layout_constraintDimensionRatio="1:1.41"

<?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:background="@color/orange">

    <!-- divider line which i used as restricting my A4 size container height-->

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/headline_title_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias=".85"/>

    <!-- A4 size Image View-->

    <ImageView
        android:id="@+id/resumeContainer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="16dp"
        android:layout_marginRight="12dp"
        android:layout_marginBottom="16dp"
        android:background="@color/green"
        android:text="@string/change"
        android:src="@drawable/banner"
        app:layout_constraintBottom_toTopOf="@id/divider"
        app:layout_constraintDimensionRatio="1:1.27"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <!-- for Bottom two buttons -->

    <com.bold.job.utils.CustomButton
        android:id="@+id/preview"
        style="@style/tertiaryButtonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:onClick="preview"
        android:text="@string/preview_resume"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/guideline"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/divider"
        />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <com.bold.job.utils.CustomButton
        android:id="@+id/preview2"
        style="@style/tertiaryButtonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:onClick="preview"
        app:layout_constraintLeft_toRightOf="@id/guideline"
        app:layout_constraintRight_toRightOf="parent"
        android:text="@string/preview_resume"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/divider"
        />


</android.support.constraint.ConstraintLayout>

Ответ 4

Обратите внимание на последнюю строку, добавив ограничения по краям, что затрудняет работу.

Вы также можете использовать представление дизайна в Studio и перетащить ограничения между объектами.

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Available chats" />

<ListView
    android:id="@+id/listChats"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/textView"/>