ConstraintLayout 1.1.0 视图不能在同一条边上相互引用?
ConstraintLayout 1.1.0 views cannot reference each other on the same edge?
以下代码过去在 1.0.2 中运行良好,但在 1.1.0 稳定版中不起作用 - 从字面上消除了布局中所有视图中所有其他约束的影响。是有原因还是只是一个怪癖?花了一段时间才找到它。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app1="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/viewOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/viewTwo" <-- culprit
tools:text="View one"/>
<TextView
android:id="@+id/viewTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app1:layout_constraintTop_toBottomOf="@+id/viewOne" <-- culprit
tools:text="View two"/>
</android.support.constraint.ConstraintLayout>
删除其中一个罪魁祸首约束会使 1.1.0 中的一切恢复正常。
将app:layout_constraintTop_toTopOf="parent"
添加到viewOne
,它将再次起作用。您也可以删除 app:layout_constraintBottom_toTopOf="@+id/viewTwo"
,因为不需要它,所以什么都不会改变。
虽然这两个视图在垂直方向上受到约束,但它们相互约束并且没有将它们与容器联系起来。如果没有其他限制,该组将默认滑到顶部。看起来两者都将在 1.1.0 中滑到顶部,并在 1.0.2 中排在另一个下方。这可能只是视图定义方式的副作用。
无论如何,XML 的格式不正确,所有视图都应直接或间接地约束到包含 ConstraintLayout
的内容。进行上述更改,一切都会好起来的。
只需删除
应用:layout_constraintBottom_toTopOf="@+id/viewTwo"
从上面的 xml 代码开始,你就可以开始了。
下面给出的代码运行良好。
<?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:app1="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/viewOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="View one" />
<TextView
android:id="@+id/viewTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app1:layout_constraintTop_toBottomOf="@+id/viewOne"
tools:text="View two" />
</android.support.constraint.ConstraintLayout>
所以只针对你的情况
app:layout_constraintBottom_toTopOf="@+id/viewTwo" 这就是罪魁祸首。
以下代码过去在 1.0.2 中运行良好,但在 1.1.0 稳定版中不起作用 - 从字面上消除了布局中所有视图中所有其他约束的影响。是有原因还是只是一个怪癖?花了一段时间才找到它。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app1="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/viewOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/viewTwo" <-- culprit
tools:text="View one"/>
<TextView
android:id="@+id/viewTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app1:layout_constraintTop_toBottomOf="@+id/viewOne" <-- culprit
tools:text="View two"/>
</android.support.constraint.ConstraintLayout>
删除其中一个罪魁祸首约束会使 1.1.0 中的一切恢复正常。
将app:layout_constraintTop_toTopOf="parent"
添加到viewOne
,它将再次起作用。您也可以删除 app:layout_constraintBottom_toTopOf="@+id/viewTwo"
,因为不需要它,所以什么都不会改变。
虽然这两个视图在垂直方向上受到约束,但它们相互约束并且没有将它们与容器联系起来。如果没有其他限制,该组将默认滑到顶部。看起来两者都将在 1.1.0 中滑到顶部,并在 1.0.2 中排在另一个下方。这可能只是视图定义方式的副作用。
无论如何,XML 的格式不正确,所有视图都应直接或间接地约束到包含 ConstraintLayout
的内容。进行上述更改,一切都会好起来的。
只需删除
应用:layout_constraintBottom_toTopOf="@+id/viewTwo"
从上面的 xml 代码开始,你就可以开始了。
下面给出的代码运行良好。
<?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:app1="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/viewOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="View one" />
<TextView
android:id="@+id/viewTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app1:layout_constraintTop_toBottomOf="@+id/viewOne"
tools:text="View two" />
</android.support.constraint.ConstraintLayout>
所以只针对你的情况 app:layout_constraintBottom_toTopOf="@+id/viewTwo" 这就是罪魁祸首。