我已经更新了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
上发出警告,我该怎么办?
就像在图片中显示连接一样。在约束布局中,我们可以将小部件放在任何地方但在应用程序上运行小部件不在你在android studio中开发期间放置的位置。要解决此问题,请确保小部件同时具有水平和垂直通过从边连接拖动来实现约束。窗口小部件在每一侧都有圆形,拖动它以调整以固定其位置。
出现此问题的原因是您已使用并且您的视图没有任何约束
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"
它只在设计时(在Android Studio的预览中)效果(此处的效果是您的视图将保留8dp和页边距8dp),在运行时它不会产生任何影响
要解决此警告,您应该为布局添加足够的约束或简单地删除tools:layout_editor_absoluteY="8dp"
和tools:layout_editor_absoluteX="8dp"
当您的布局或窗口小部件没有约束代码但需要安装android视图时,会发生此警告。例如,当您使用android:layout_marginTop =“12dp”时,您必须添加app:layout_constraintTop_toTopOf =“”,否则会出现警告。