我注意到ConstraintLayout行为从1.1.2到1.1.3发生了奇怪的变化,这可能会导致布局出现很多问题。另外,我个人认为这是一个错误,因为这种行为应该是错误的。
检查以下布局:
<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">
<Button
android:id="@+id/test1_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test1"
app:layout_constraintTop_toTopOf="@id/test2_btn"
app:layout_constraintBottom_toBottomOf="@id/test2_btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/test2_btn"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintHorizontal_bias="1"/>
<Button
android:id="@+id/test2_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/test1_btn"/>
</android.support.constraint.ConstraintLayout>
这是在版本1.1.2
和版本中呈现此布局1.1.3
的方式ConstraintLayout
:
现在我们添加android:visibility="gone"
到test1_btn
。在ConstraintLayout
version中1.1.2
,布局呈现如下:
This is completely logical, because we have app:layout_constraintHorizontal_bias="1"
set, so test2_btn
should stay at far right of the chain. Now if we use ConstraintLayout
version 1.1.3
, layout will be rendered as follows:
Just what happened? Why I lost chain bias?
To get the old behavior while still using version 1.1.3, add this attribute to your <ConstraintLayout>
tag:
app:layout_optimizationLevel="direct|barrier"
Version 1.1 of the ConstraintLayout library introduced new optimizations. These optimizations parse your ConstraintLayout and look for constraints that can be removed or simplified.
从1.1.2版开始,默认情况下唯一启用的优化是direct和barrier。版本1.1.3也 默认启用链。您可以通过手动指定应启用的优化来返回1.1.2行为。
为了证明这是真正的问题,我尝试使用版本1.1.2启用链优化。
第一个屏幕截图是使用1.1.2版和您发布的布局(android:gone
添加了属性)拍摄的。然后,我将此属性添加到根ConstraintLayout
标签:
app:layout_optimizationLevel="chains"
现在,我看到了与您在1.1.3版中发现的相同的行为:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句