ConstraintLayout layout_constraintDimensionRatio不起作用

pis*_*ffe 16 android android-constraintlayout

我使用了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>
Run Code Online (Sandbox Code Playgroud)

截图

我引用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" />
Run Code Online (Sandbox Code Playgroud)

将按钮的高度设置为与其宽度相同."

- >但我没有工作.

Ram*_*mli 27

你忘了添加你的约束

<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>
Run Code Online (Sandbox Code Playgroud)

0dp仅应用于ConstraintLayout的子视图.任何视图都应该应用其父级的属性规则.


Koe*_*ren 7

关于版本1.1.0,这已经改变了.

您现在可以定义:

app:layout_constraintDimensionRatio="1:1"
app:layout_constraintDimensionRatio="W,1:1"
app:layout_constraintDimensionRatio="H,1:1"
Run Code Online (Sandbox Code Playgroud)

请查看以下链接,查找有关DimensionConstraints的所有文档:

链接到文档