6

我已经更新了 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,我该怎么办?

4

7 回答 7

9

转到设计,右键单击相关布局并选择约束布局->推断约束在此处输入图像描述

于 2017-03-30T13:26:31.427 回答
3

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

看图片示例

在此处输入图像描述

于 2017-12-21T18:54:26.673 回答
1

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

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

于 2018-01-18T04:54:52.223 回答
0

发生此问题是因为您已使用并且您的视图没有任何约束

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

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

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

于 2017-04-05T04:22:24.543 回答
0

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

于 2019-02-21T02:13:04.767 回答
0

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

于 2019-09-22T04:28:42.667 回答
0

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

但是,如果您仍然坚持这种实现,那么实际问题就出在:

您在 RelativeLayout 中使用了tools:layout_editor_absoluteXandtools: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 将受到垂直(顶部和底部)和水平(开始和结束)的约束。

我希望这有帮助。快乐编码!

于 2019-09-22T11:32:49.433 回答