在Android Studio中使用约束布局时的奇怪后果

时间:2017-07-08 06:59:40

标签: android android-layout android-constraintlayout

我正处于Android开发阶段。最近,为了练习布局设计,我正在尝试建立一个简单计算器的模拟。我在“设计”选项卡中创建并放置了视图(Buttons,EditTexts)。然后我陷入了“这个视图没有约束”的错误,所以我搜索了StackOverflow,最后发现解决方案是右键单击小部件 - >约束布局 - >推断约束。但是当我这样做时,我程序中的那些视图变化很大,我无法将它们修改为原始位置和大小。如何在不更改视图位置和大小的情况下解决错误?

在使用约束布局之前,这是我的计算器:

before_constraint:

enter image description here

使用后这是我的计算器:

after_constraint:

enter image description here

这是我的源代码:

<?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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.huy9515gmail.helloworld.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">


<Button
    android:id="@+id/button10"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="0"
    tools:layout_editor_absoluteX="87dp"
    tools:layout_editor_absoluteY="186dp" />

<Button
    android:id="@+id/button7"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="7"
    tools:layout_editor_absoluteX="34dp"
    tools:layout_editor_absoluteY="141dp" />

<Button
    android:id="@+id/button8"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="8"
    tools:layout_editor_absoluteX="87dp"
    tools:layout_editor_absoluteY="141dp" />

<Button
    android:id="@+id/button9"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="9"
    tools:layout_editor_absoluteX="140dp"
    tools:layout_editor_absoluteY="141dp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My First Calculator"
    tools:layout_editor_absoluteX="134dp"
    tools:layout_editor_absoluteY="16dp" />

<Button
    android:id="@+id/button"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="2"
    tools:layout_editor_absoluteX="87dp"
    tools:layout_editor_absoluteY="51dp" />

<Button
    android:id="@+id/button3"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="3"
    tools:layout_editor_absoluteX="140dp"
    tools:layout_editor_absoluteY="51dp" />

<Button
    android:id="@+id/button4"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="/"
    tools:layout_editor_absoluteX="281dp"
    tools:layout_editor_absoluteY="96dp" />

<Button
    android:id="@+id/button14"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="x"
    tools:layout_editor_absoluteX="228dp"
    tools:layout_editor_absoluteY="96dp" />

<Button
    android:id="@+id/button13"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="-"
    tools:layout_editor_absoluteX="281dp"
    tools:layout_editor_absoluteY="51dp" />

<Button
    android:id="@+id/button12"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="+"
    tools:layout_editor_absoluteX="228dp"
    tools:layout_editor_absoluteY="51dp" />

<Button
    android:id="@+id/button11"
    android:layout_width="106dp"
    android:layout_height="40dp"
    android:text="Delete"
    tools:layout_editor_absoluteX="228dp"
    tools:layout_editor_absoluteY="210dp" />

<Button
    android:id="@+id/button16"
    android:layout_width="106dp"
    android:layout_height="40dp"
    android:text="Calculate"
    tools:layout_editor_absoluteX="228dp"
    tools:layout_editor_absoluteY="170dp" />

<Button
    android:id="@+id/button15"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="1"
    tools:layout_editor_absoluteX="34dp"
    tools:layout_editor_absoluteY="51dp" />

<Button
    android:id="@+id/button2"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="4"
    tools:layout_editor_absoluteX="34dp"
    tools:layout_editor_absoluteY="96dp" />

<Button
    android:id="@+id/button5"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="5"
    tools:layout_editor_absoluteX="87dp"
    tools:layout_editor_absoluteY="96dp" />

<Button
    android:id="@+id/button6"
    android:layout_width="53dp"
    android:layout_height="45dp"
    android:text="6"
    tools:layout_editor_absoluteX="140dp"
    tools:layout_editor_absoluteY="96dp" />

<EditText
    android:id="@+id/editText"
    android:layout_width="310dp"
    android:layout_height="98dp"
    android:ems="10"
    android:inputType="textPersonName"
    tools:layout_editor_absoluteX="37dp"
    tools:layout_editor_absoluteY="327dp" />

2 个答案:

答案 0 :(得分:0)

虽然他们都有设计时的职位,但你的意见都没有受到限制。关于运行时限制,请查看此documentationThis也是一个很好的介绍。

设计时的位置是这样的:

Hashtag::find()->where(['BINARY(`tag`)' => $expTags])->select(["id"]);

此代码将您的视图定位在设计器中。运行时约束如下所示:

tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp"

并将在运行时(以及在设计器中)定位视图。

不幸的是,当你推断出约束时,发生了一些不好的事情。设计师做出了最好的猜测,但事实并非如此。

我建议你逐个视图进行布局,删除设计时约束并添加运行时约束,直到你的布局看起来像你想要的那样。

答案 1 :(得分:0)

当您“推断约束”时,会出现硬编码值。我建议您删除所有布局并重新开始,了解Constraint Layout的工作原理。如果您看到&#34;此视图不受约束“错误,Android Studio告诉您的是:视图需要更多约束。

记住iOS上的AutoLayout和Android上的ConstraintLayout的核心规则:每个视图都需要知道自己的位置(水平和垂直),还需要知道它的大小,宽度和宽度和高度。

所以,想想你的布局......我会给你一个例子。

从顶部开始,您希望“标签”以文本“欢迎使用我的计算器”为中心。

好,现在引擎需要知道4件事:

  1. 我在X轴上放哪个标签?
  2. 我在Y轴上的哪个地方贴上了这个标签?
  3. 此标签的宽度是多少?
  4. 这个标签的高度是多少?
  5. 有一些方法可以让引擎推断其中一些(我们将会看到),但在大多数情况下,需要这四个 per查看(无论是推断还是显式)。

    那么让我们从那里开始你的“标题”。

    TextView将居中,但我确定你希望它能够跨越它所能找到的所有宽度(并在需要时进行压缩)。由于ConstraintLayout没有match_parent(原因很明显:a.k.a:想法是你必须设置约束),我们必须能够以不同的方式做到这一点......

    对于X轴(水平),我们希望textView固定到start的{​​{1}}(在这种情况下也称为根),以及parent父母也是如此(有效地使用所有屏幕,减去你决定在任何一方添加的任何边距)。

    到目前为止,有两个限制因素。为了使上述工作正常,ConstraintLayout表示您必须在end中使用0dp的“特殊”值。这告诉引擎宽度将是“我计算所有约束后我将不知道的任何值”。

    好。现在我们已经解决了#1和#3。

    对于#2和#4,你可以做类似的事情......或者你可以享受一些观点,推断出值的事实。例如:文本视图不需要高度,它们会根据字体大小,字符数等自动调整大小,但您仍然需要告诉它width轴的哪个位置将显示此文本视图

    在您的情况下,我只是将文字视图的Y与您的top对齐。请注意,您无需告诉布局将文本parent.top固定为任何内容,因为它的高度为bottom

    等等......

    现在转过the official docs并开始阅读更多关于你可以做的事情(例如,链子可能在这里派上用场)。

相关问题