【问题标题】:Constraint Layout gives error in default template in Android Studio 2.3约束布局在 Android Studio 2.3 中的默认模板中给出错误
【发布时间】:2017-08-24 09:54:07
【问题描述】:

我更新了 Android Studio 2.3,之后我将默认 ConstrainLayout 作为模板 xml。

但我有 RelativeLayout 作为子布局,我收到以下警告。

此视图不受约束,它只有设计时位置,因此除非您添加约束,否则它将跳转到 (0,0)。

布局编辑器允许您将小部件放置在画布上的任何位置, 它用设计时间属性记录当前位置(例如 作为 layout_editor_absoluteX。)这些属性不适用于 运行时,因此如果您在设备上推送布局,小部件可能 出现在与编辑器中不同的位置。为了解决这个问题, 确保小部件同时具有水平和垂直约束 从边缘连接中拖动。

<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.pratikbutani.demoapp.MainActivity"
        tools:showIn="@layout/activity_main">

        <RelativeLayout 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:id="@+id/content_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:showIn="@layout/activity_main"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="8dp">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true" />

        </RelativeLayout>

    </android.support.constraint.ConstraintLayout>
</layout>

收到RelativeLayout 的警告,我该怎么办?

【问题讨论】:

  • 为了获得更好的布局性能,您不应在约束布局内嵌入相对布局。 ConstraintLayout 本身完成了相对布局所做的一切。可以直接在Constraint Layout下添加RecyclerView。

标签: android android-layout android-constraintlayout android-studio-2.3


【解决方案1】:

首先,由于您使用的是 ConstraintLayout,因此您不需要在其中嵌入子 RelativeLayout。我的意思是,这就像同时驾驶两辆汽车一样,是多余的。您应该仅使用 RelativeLayout 或仅使用 ConstraintLayout。

但是,如果你仍然坚持这个实现,那么实际问题就在这里:

您在RelativeLayout 中使用了tools:layout_editor_absoluteX 和tools:layout_editor_absoluteY 元素,并且RelativeLayout 没有受到限制。因此,无法在运行时确定 absoluteX 和 absoluteY 的参考点。要解决这个问题,只需将这些属性添加到您的 RelativeLayout 中,将 RelativeLayout 垂直和水平地约束到 ConstraintLayout:

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"

这样,您的 RelativeLayout 将受到垂直(顶部和底部)和水平(开始和结束)的约束。

我希望这会有所帮助。编码愉快!

【讨论】:

    【解决方案2】:

    你的相对布局不受约束,约束布局和相对布局都是独立的。转到“设计/文本”选项卡以正确定位。您也可以考虑仅使用两种布局之一;约束布局和相对布局一样好用

    【讨论】:

      【解决方案3】:

      当您的布局或小部件没有约束代码但需要android视图时会出现此警告。例如,当您使用android:layout_marginTop =“12dp”时,您必须添加app:layout_constraintTop_toTopOf =“”,否则会出现警告。

      【讨论】:

        【解决方案4】:

        出现这个问题是因为你已经使用并且你的视图没有任何约束

        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp"
        

        它仅在设计时(在 Android Studio 的预览版中)起作用(这里的效果是您的视图将边距左 8dp 和上边距 8dp),在运行时它不会有任何影响

        要解决此警告,您应该为布局添加足够的约束或简单地删除 tools:layout_editor_absoluteY="8dp" 和 tools:layout_editor_absoluteX="8dp"

        【讨论】:

          【解决方案5】:

          如图所示建立连接。在约束布局中,我们可以将小部件放置在任何地方,但在应用程序上运行时,小部件与您在 android studio 开发期间放置的位置不同。要解决此问题,请确保小部件同时具有通过从边缘连接拖动来进行水平和垂直约束。小部件的每一侧都有类似圆形的东西,拖动它来调整以固定其位置。

          https://i.stack.imgur.com/r8L8O.png

          【讨论】:

            【解决方案6】:

            拖动对象以连接到墙壁...在墙壁和对象之间创建连接

            看图片示例

            【讨论】:

            • 正是我想要的。快速简单!想知道为什么这不是公认的答案。
            【解决方案7】:

            进入Design,右键相关布局,选择Constraint layout-->Infer Constraints

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-05-31
              • 2016-08-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-06-05
              • 1970-01-01
              相关资源
              最近更新 更多