首页 > 解决方案 > 约束布局版本 > 2.0.1 不遵循公会规则

问题描述

尝试使用最新版本的约束布局时遇到布局问题。它似乎不遵循指导规则。这是库中的错误吗?我的项目使用了许多指导方针,要改变每一个指导方针将是一个巨大的痛苦。任何人都知道是否有解决方法我做错了吗?此布局问题似乎发生在高于约束布局 2.0.1 的版本中。

约束布局 v 1.1.3 约束布局 v 1.1.3

约束布局 v 2.0.4 - 位置图标在我的小部件约束布局后面 约束布局 v 2.0.4

MainActivity 布局 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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffffff"
    android:clickable="true"
    android:focusable="true">

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/edit_text_message_input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="@null"
        android:hint="Please Type Something"
        android:maxHeight="100dp"
        android:minHeight="56dp"
        android:paddingStart="16dp"
        android:paddingTop="8dp"
        android:paddingEnd="16dp"
        android:paddingBottom="13dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <requestFocus />
    </androidx.appcompat.widget.AppCompatEditText>


    <RelativeLayout
        android:id="@+id/mic_container"
        android:layout_width="140dp"
        android:layout_height="140dp"
        app:layout_constraintTop_toBottomOf="@+id/edit_text_message_input"
        app:layout_constraintBottom_toTopOf="@+id/edit_text_message_input"
        app:layout_constraintLeft_toLeftOf="@+id/mic_icon_guideline"
        app:layout_constraintRight_toRightOf="@+id/mic_icon_guideline">

        <CheckBox
            android:layout_centerInParent="true"
            android:id="@+id/button_nav"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:button="@drawable/navigation_button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </RelativeLayout>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/mic_icon_guideline"
        app:layout_constraintGuide_end="28dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/attachment_icon_container"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edit_text_message_input">

        <CheckBox
            android:id="@+id/button_add_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:button="@drawable/add_location_button"
            app:layout_constraintLeft_toRightOf="@+id/button_remove_location"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <CheckBox
            android:id="@+id/button_remove_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:button="@drawable/remove_location_button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/button_add_location"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

build.gradle 文件

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.test_constraint_project"
        minSdkVersion 28
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "androidx.core:core-ktx:1.3.2"
    implementation "androidx.appcompat:appcompat:1.2.0"
    implementation "com.google.android.material:material:1.2.1"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    //implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

标签: androidandroid-constraintlayout

解决方案


RelativeLayout 具有以下mic_container约束:

app:layout_constraintTop_toBottomOf="@+id/edit_text_message_input"
app:layout_constraintBottom_toTopOf="@+id/edit_text_message_input"

一个视图的顶部应该限制在另一个视图的底部,而第一个视图的底部应该限制在第二个视图的顶部,这似乎是不对的。这确实是一种无效的情况,我认为您看到ConstraintLayout在两个版本之间以不同的方式解决了这个问题。

如果您将以下内容声明为约束:

app:layout_constraintTop_toTopOf="@+id/edit_text_message_input"
app:layout_constraintBottom_toBottomOf="@+id/edit_text_message_input"

我认为你会看到mic_container适当的定位。您将必须确定这是否可以解决您的所有问题。


推荐阅读