约束布局在 Android Studio 2.3 中的默认模板中给出错误

新手上路,请多包涵

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

原文由 Pratik Butani 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 331
2 个回答

转到 Design,右键单击 relative layout 并选择 Constraint layout–>Infer Constraints 在此处输入图像描述

原文由 J. Shiv 发布,翻译遵循 CC BY-SA 3.0 许可协议

拖动对象连接到墙壁…创建墙壁和对象之间的连接

看图片示例

在此处输入图像描述

原文由 Samir Alakbarov 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题