【问题标题】:Constraint Layout not working, maybe a bug in design editor?约束布局不起作用,可能是设计编辑器中的错误?
【发布时间】:2018-01-02 15:21:47
【问题描述】:

我正在使用 android studio 创建我的第一个应用程序,这是我的第一个问题:

我想试试 ConstraintLayout。我已经使用设计编辑器(一起单击)使用 ConstraintLayout 构建了布局。当我在 Android Emulator 中尝试布局时,所有按钮都移到了左上角:( 除了我创建第一个项目时自动生成的“Hello World”标签。

Label 和 Button 的区别在于,缺少一些以 app:layout_constraint.... 开头的代码行。你可以在代码中看到它。

我做错了什么,还是一个错误? 我很高兴得到答案! :)

<?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.example.u0017007.coffeecounter.MainActivity">

    <TextView
        android:layout_width="136dp"
        android:layout_height="30dp"
        android:text="Hello World!"
        android:textSize="24sp"
        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.234" />

    <Button
        android:id="@+id/buttonAddCoffee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:freezesText="false"
        android:text="@string/add_coffee"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="231dp" />

    <Button
        android:id="@+id/buttonRemoveCoffee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/remove_coffee"
        tools:layout_editor_absoluteX="236dp"
        tools:layout_editor_absoluteY="231dp" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

标签: android layout constraints android-constraintlayout


【解决方案1】:

从上面的代码看来,您没有为按钮添加任何约束,这就是它们移动到左上角的原因。

<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">

    <TextView
        android:layout_width="136dp"
        android:layout_height="30dp"
        android:text="Hello World!"
        android:textSize="24sp"
        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.234" />

    <Button
        android:id="@+id/buttonAddCoffee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:freezesText="false"
        android:text="ADD coffee"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="276dp"
        app:layout_constraintRight_toLeftOf="@+id/buttonRemoveCoffee"
        android:layout_marginRight="82dp"
        android:layout_marginLeft="24dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="1.0" />

    <Button
        android:id="@+id/buttonRemoveCoffee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="remove coffee"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="276dp"
        android:layout_marginRight="24dp"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

请参考以下链接 -

https://www.youtube.com/watch?v=z53Ed0ddxgM

【讨论】:

    【解决方案2】:

    tools:layout_editor_absoluteXtools:layout_editor_absoluteY 只用于预览,就像所有tools:XXXX一样。
    您需要在视图中添加约束。您可以添加 XML,也可以使用可视化编辑器进行添加。

    有一个很好的website 解释了所有关于ConstraintsLayout

    顺便说一句,如果你不设置约束,Android Studio 会以错误This view is not constrained, it only has designtime positions, so it will jump to (0,0) unless you add constraints 温暖你。

    【讨论】:

      【解决方案3】:

      工具:layout_editor_absoluteX="236dp"

      工具:layout_editor_absoluteY="231dp"

      这些仅由工作室用于在图形编辑器中渲染。在运行期间,

      由于没有设置约束,视图采用默认位置(0,0)。

      尝试对按钮设置一些约束并进行处理

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多