【发布时间】: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