使用math_parent和0dp作为宽度时,高度为0dp的ConstraintLayout视图具有不同的实际高度

问题描述 投票:1回答:1

这个问题可以用一个例子更好地描述:

“”

您可以在生成的渲染布局中看到,first_viewsecond_view的高度不同,唯一不同的是first_view使用的是

  • layout_width=match_parent

而不是

  • layout_width="0dp";
  • layout_constraintStart_toStartOf="parent";
  • layout_constraintEnd_toEndOf="parent"

是错误还是预期的行为?

android kotlin android-constraintlayout
1个回答
1
投票
取自official docs

重要:不建议将MATCH_PARENT用于包含在ConstraintLayout。可以通过使用定义类似的行为MATCH_CONSTRAINT与相应的左/右或上/下约束设置为“父”。

official training

注意:您不能对ConstraintLayout中的任何视图使用match_parent。而是使用“匹配约束”(0dp)。

话虽如此,您的示例也是无效的,因为您对高度使用0dp(匹配约束),但未指定底部约束,这可能会导致视图的意外行为。要匹配维度的约束,您需要声明两端。

© www.soinside.com 2019 - 2024. All rights reserved.