0

我的项目使用的是 Constraintlayout 1.1.3,我刚刚将其版本更新为 2.0.4。

整个布局的一部分有点混乱。所以我检查了我的代码,我意识到我使用 layout_ConstraintLeft 或 right。

所以我添加了ConstraintLayout属性的“开始”和“结束”。但它仍然无法正常工作。

他们互相粘着。

此约束布局由平面层次结构组成。这个布局的另一部分仍然有效。

为什么它不起作用?

我该如何解决?

这就是我要的。 在此处输入图像描述

但它最终就像 tihs 一样。他们互相粘着。当我升级 ConstraintLayout 版本时。 在此处输入图像描述

这是我的 xml 代码。

 <?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:custom="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#FFF"
 android:fillViewport="true"
 android:clickable="true"
 tools:context=".ManiActivity">

<ImageView
    android:id="@+id/container_outline"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#333"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHeight_percent="0.25"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0"
    tools:viewBindingIgnore="true" />

<ImageView
    android:id="@+id/iv_drawer_info_logo"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:src="@drawable/img_drawer_logo"
    app:layout_constraintBottom_toBottomOf="@id/container_outline"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintHeight_percent="0.15"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintLeft_toLeftOf="@id/container_outline"
    app:layout_constraintStart_toStartOf="@id/container_outline"
    app:layout_constraintRight_toRightOf="@id/container_outline"
    app:layout_constraintEnd_toEndOf="@id/container_outline"
    app:layout_constraintTop_toTopOf="@id/container_outline"
    tools:viewBindingIgnore="true" />

<ImageView
    android:id="@+id/iv_drawer_info_name_icon"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginLeft="10dp"
    android:paddingTop="1dp"
    android:src="@drawable/icn_drawer_name"
    app:layout_constraintBottom_toBottomOf="@id/tv_drawer_info_name"
    app:layout_constraintDimensionRatio="3:5"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintLeft_toRightOf="@id/iv_drawer_info_logo"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@id/tv_drawer_info_name"
    app:layout_constraintVertical_bias="1"
    tools:viewBindingIgnore="true" />

<TextView
    android:id="@+id/tv_drawer_info_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:gravity="left|bottom"
    android:textColor="#FFF"
    android:textSize="@dimen/text_main_big"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@id/tv_drawer_info_id"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintLeft_toRightOf="@id/iv_drawer_info_name_icon"
    app:layout_constraintRight_toRightOf="@id/container_outline"
    app:layout_constraintTop_toTopOf="@id/container_outline"
    app:layout_constraintVertical_chainStyle="packed"
    tools:text="Hello" />

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tv_drawer_info_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/detail_margin_3"
    android:ellipsize="end"
    android:maxLines="1"
    android:textColor="#FFF"
    android:textSize="14dp"
    app:autoSizeMaxTextSize="14dp"
    app:autoSizeMinTextSize="9dp"
    app:autoSizeTextType="uniform"
    app:layout_constrainedWidth="true"
    app:layout_constraintBottom_toBottomOf="@id/container_outline"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintLeft_toLeftOf="@id/iv_drawer_info_name_icon"
    app:layout_constraintStart_toStartOf="@id/iv_drawer_info_name_icon"
    app:layout_constraintRight_toLeftOf="@id/iv_drawer_info_expire_icon"
    app:layout_constraintEnd_toStartOf="@id/iv_drawer_info_expire_icon"
    app:layout_constraintTop_toBottomOf="@id/tv_drawer_info_name"
    tools:text="hello wordhello wordhello wordhello wordhello wordhello wordhello wordhello word" />

<ImageView
    android:id="@+id/iv_drawer_info_expire_icon"
    android:layout_width="20dp"
    android:layout_height="0dp"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginRight="3dp"
    android:layout_marginEnd="3dp"
    android:paddingTop="1dp"
    android:src="@drawable/icn_drawer_expire"
    app:layout_constraintBottom_toBottomOf="@id/tv_drawer_info_expire_title"
    app:layout_constraintHorizontal_bias="1"
    app:layout_constraintLeft_toLeftOf="@id/container_outline"
    app:layout_constraintStart_toStartOf="@id/container_outline"
    app:layout_constraintRight_toLeftOf="@id/tv_drawer_info_expire_title"
    app:layout_constraintEnd_toStartOf="@id/tv_drawer_info_expire_title"
    app:layout_constraintTop_toTopOf="@id/tv_drawer_info_expire_title"
    app:layout_constraintVertical_bias="1"
    tools:viewBindingIgnore="true" />


<TextView
    android:id="@+id/tv_drawer_info_expire_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left|bottom"
    android:text="Hello word"
    android:textColor="#FFF"
    android:textSize="14dp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@id/tv_drawer_info_expire"
    app:layout_constraintHorizontal_bias="0.6"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@id/container_outline"
    app:layout_constraintVertical_chainStyle="packed"
    tools:viewBindingIgnore="true" />

<TextView
    android:id="@+id/tv_drawer_info_expire"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/detail_margin_3"
    android:textColor="#FFF"
    android:textSize="14dp"
    app:layout_constraintBottom_toBottomOf="@id/container_outline"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintLeft_toLeftOf="@id/iv_drawer_info_expire_icon"
    app:layout_constraintStart_toStartOf="@id/iv_drawer_info_expire_icon"
    app:layout_constraintRight_toRightOf="@id/container_outline"
    app:layout_constraintEnd_toEndOf="@id/container_outline"
    app:layout_constraintTop_toBottomOf="@id/tv_drawer_info_expire_title"
    tools:text="~2020.12.31" />

<ImageButton
    android:id="@+id/btn_drawer_pay"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginRight="24dp"
    android:background="@drawable/ripple_white"
    android:padding="15dp"
    android:scaleType="fitCenter"
    android:src="@drawable/img_drawer_payment"
    app:layout_constrainedWidth="true"
    app:layout_constraintBottom_toBottomOf="@id/iv_drawer_info_logo"
    app:layout_constraintDimensionRatio="4:3"
    app:layout_constraintHeight_percent="0.15"
    app:layout_constraintHorizontal_bias="1"
    app:layout_constraintLeft_toLeftOf="@id/container_outline"
    app:layout_constraintStart_toStartOf="@id/container_outline"
    app:layout_constraintRight_toRightOf="@id/container_outline"
    app:layout_constraintEnd_toEndOf="@id/container_outline"
    app:layout_constraintTop_toTopOf="@id/iv_drawer_info_logo" />

</androidx.constraintlayout.widget.ConstraintLayout>
4

1 回答 1

1

我不清楚为什么您的布局不起作用或为什么在升级ConstraintLayout时它停止工作。我建议您将布局分解为其基本组件并重新构建为完整布局。

考虑到您发布的图像,以下是我将如何使用ConstraintLayout 来解决此问题:

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.15"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintHeight_percent="0.15"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintEnd_toStartOf="@+id/textView2"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        tools:layout_editor_absoluteY="172dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintEnd_toStartOf="@+id/imageView2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView"
        tools:layout_editor_absoluteY="172dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

此 XML 生成以下图像:

在此处输入图像描述

除此之外,请检查您拥有的所有约束是否都是您想要的,并且您了解它们在做什么。从基本布局开始,一次添加一个组件或一小组组件将是解决问题的合理方法。

于 2020-12-31T01:25:02.137 回答