约束布局版本 > 2.0.1 不遵循 guildline 规则
Constraint Layout version > 2.0.1 does not follow guildline rules
我在尝试使用最新版本的约束布局时遇到布局问题。它似乎不遵循指导原则。这是库中的错误吗?我的项目使用了很多指导方针,要改变每一个都将是一个巨大的痛苦。任何人都知道我是否做错了?此布局问题似乎发生在高于约束布局 2.0.1 的版本中。
约束布局 v 1.1.3
约束布局 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'
}
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
的位置合适。您将必须确定这是否解决了您所有的问题。
我在尝试使用最新版本的约束布局时遇到布局问题。它似乎不遵循指导原则。这是库中的错误吗?我的项目使用了很多指导方针,要改变每一个都将是一个巨大的痛苦。任何人都知道我是否做错了?此布局问题似乎发生在高于约束布局 2.0.1 的版本中。
约束布局 v 1.1.3
约束布局 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'
}
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
的位置合适。您将必须确定这是否解决了您所有的问题。