左上角的ConstraintLayout视图

时间:2017-03-04 08:50:44

标签: android android-layout android-view android-constraintlayout view-hierarchy

每当我在Button中创建TextViewConstraintLayout等视图时,它们都会卡在顶角而不是我放置的位置。

我尝试创建新活动并更改模拟器,但结果仍然相同。

这是发生了什么的屏幕截图:

enter image description here

可能是什么问题?

5 个答案:

答案 0 :(得分:29)

Constraint Layout guides中所述:

  

如果在设备上运行布局时视图没有约束,则会在位置[0,0](左上角)绘制它。

     

您必须为视图添加至少一个水平约束和一个垂直约束。

我猜你没有应用任何约束。

手动将约束应用于视图,或让布局编辑器使用“Infer constraints”按钮为您执行此操作:

enter image description here

答案 1 :(得分:5)

  

当您将视图拖放到布局编辑器中时,即使它没有约束,它也会保留在您离开的位置...这只是为了使编辑更容易

换句话说,视图将会留在您离开的地方"直到你添加约束。

它在预览与您的应用中看起来不同的原因是因为XML tools:中的任何内容都是going to be removed from the code when your app runs。这些选项仅适用于Android Studio布局编辑器,不代表您在代码运行时应该看到的内容。

这对初学者来说可能很麻烦,因为

  

缺少的约束不会导致编译错误

<强>然而

  

布局编辑器将缺少的约束指示为工具栏中的错误。要查看错误和其他警告,请单击显示警告和错误(带编号的红色按钮)。为了帮助您避免遗漏约束,布局编辑器可以使用Autoconnect and infer constraints features

自动为您添加约束

More details

答案 2 :(得分:3)

快速,简单的解决方法是单击组件(TextView,ImageView等)并单击推断约束按钮。它看起来像2个黄色加号。

推断约束图像

infer constraints image

答案 3 :(得分:0)

将这个在xml中添加到按钮等

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.57"

然后在设计中使用它。

祝你好运! :)

答案 4 :(得分:0)

基本上,当您使用 ConstraintLayout 时,会发生这种类型的问题。

  • 因此请从Android Studio转到预览

  • 选择要在特定位置设置的按钮

  • 最后,请点击我在推断约束按钮 下图是一个红色圆圈。

https://i.imgur.com/sDu1ghP.jpg

多数民众赞成在运行项目并看到魔术

谢谢

相关问题