关于android:ConstraintLayout layout_constraintDimensionRatio不起作用

ConstraintLayout layout_constraintDimensionRatio not working

我使用了ConstraintLayout和layout_constraintDimensionRatio =" 1:1"
(宽度为warp_content,高度为0dp(match_constraint))

结果,我期望宽度和高度为1:1,但无法正常工作。

怎么了??

我附上了代码和屏幕截图。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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>

屏幕截图

我引用有关Constraintlayout的android开发者网站。
https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints

"比率::
您还可以将小部件的一个尺寸定义为另一尺寸的比例。 为此,您需要至少将一个约束尺寸设置为0dp(即MATCH_CONSTRAINT),然后将layout_constraintDimentionRatio属性设置为给定的比例。 例如:

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

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

->但是我没有工作。


您忘记添加约束

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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的子视图。
任何视图都应应用其父级的属性规则。


从版本1.1.0开始,此更改。

您现在可以定义:

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

检查下面的链接以找到有关DimensionConstraints的所有文档:

链接到文档


就我而言,我遇到一个问题,例如我必须用A4尺寸的纸张比例填充容器中的布局。

问题

我正在从后端获取A4尺寸的简历页面作为图像,因此我必须在Viewpager中附加这些图像,在这些图像中我正在使用ImageView显示这些图像。

我浏览了其中的Ratio部分的约束布局文档。所以我可以使用layout_constraintDimensionRatio解决我的问题。

enter image description here

因此,下面是我用于显示整个布局的xml,在我的情况下,我使用app:layout_constraintDimensionRatio="1:1.27" as with:height ratio,但实际比率是app:layout_constraintDimensionRatio="1:1.41"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?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>

请注意最后一行,在边缘周围添加约束使约束起作用。

您还可以在Studio中使用设计视图,并拖放对象之间的约束。

1
2
3
4
5
6
7
8
9
10
11
<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"/>