在最新版本的约束布局Beta 5中,使用match_parent会引发exception:
android.view.InflateException: Binary XML file line #12: MATCH_PARENT is not supported in ConstraintLayout
如发行说明中所述:
“(…)它的行为未定义。为了减少错误的风险,如果我们遇到它,我们现在抛出exception。” – 来源
他们建议正确的用法是使用0dp(MATCH_CONSTRAINT),但由于我的约束布局中有抽屉布局,因此将宽度设置为0dp会抛出“DrawerLayout必须使用MeasureSpec.EXACTLY测量”错误。
所以我的问题是如何抑制错误“ConstraintLayout不支持MATCH_PARENT”?
更换
android:layout_width="match_parent"
在ConstraintLayout中可以完成
android:layout_width="0dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"
即将小部件约束到父级的右边缘和左边缘
至少从版本1.1.0-beta3的约束布局开始,
您可以毫无问题地使用match_parent。
但是仍然建议您使用:
android:layout_width="0dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"