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

Pra*_*ani 6 android android-layout android-constraintlayout 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>
Run Code Online (Sandbox Code Playgroud)

警告RelativeLayout,我该怎么办?

小智 9

转到Design,右键单击相对布局,然后选择Constraint layout - > Infer Constraints在此输入图像描述