约束布局在 Android Studio 2.3 的默认模板中给出错误
Constraint Layout gives error in default template in Android Studio 2.3
我更新了 Android Studio 2.3,之后我将默认 ConstrainLayout
作为模板 xml。
但是我有 RelativeLayout
作为子布局并且我收到以下警告。
这个视图没有约束,它只有设计时的位置,所以它会跳到 (0,0) 除非你添加约束。
The layout editor allows you to place widgets anywhere on the canvas,
and it records the current position with design time attributes (such
as layout_editor_absoluteX.) These attributes are not applied at
runtime, so if you push your layout on a device, the widgets may
appear in a different location than shown in the editor. To fix this,
make sure a widget has both horizontal and vertical constraints by
dragging from the edge connections.
<?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
收到警告,我该怎么办?
转到设计,右键单击相对布局和select约束布局-->推断约束
出现这个问题是因为你有使用并且你的视图没有任何约束
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"
它仅在设计时(在 Android Studio 的预览版中)有效(这里的效果是您的视图将留出 8dp 的边距和 8dp 的顶部边距),在运行时它不会有任何影响
要解决此警告,您应该为布局添加足够的约束或简单地删除 tools:layout_editor_absoluteY="8dp"
和 tools:layout_editor_absoluteX="8dp"
拖动对象连接到墙壁...创建墙壁和对象之间的连接
看图例
如图所示制作 connection.In 约束布局,我们可以将小部件放置在任何地方,但 运行 在应用程序上,小部件与您在 android 开发期间放置的位置不同studio.To 修复此问题,通过从边缘拖动确保小部件具有水平和垂直约束 connections.Widgets 每一侧都有圆形的东西,拖动它以调整以固定其位置。
https://i.stack.imgur.com/r8L8O.png
当您的布局或小部件没有约束代码但 android 视图 needs.For 例如,当您使用 android:layout_marginTop="12dp" 时,您必须添加 app:layout_constraintTop_toTopOf="",如果没有出现警告。
你的相对布局没有约束,约束布局和相对布局都是独立的。转到 Design/Text 选项卡以正确定位。您也可以考虑仅使用两种布局中的一种;约束布局与相对布局一样有效
首先,由于您使用的是 ConstraintLayout,因此不需要在其中嵌入子 RelativeLayout。我的意思是,这就像同时驾驶两辆车一样,这是多余的。您应该仅使用 RelativeLayout 或仅使用 ConstraintLayout。
但是,如果您仍然坚持这种实现方式,那么实际问题就出在这里:
您在 RelativeLayout 中使用了 tools:layout_editor_absoluteX
和 tools: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 将在垂直方向(顶部和底部)和水平方向(开始和结束)受到约束。
希望对您有所帮助。编码愉快!
我更新了 Android Studio 2.3,之后我将默认 ConstrainLayout
作为模板 xml。
但是我有 RelativeLayout
作为子布局并且我收到以下警告。
这个视图没有约束,它只有设计时的位置,所以它会跳到 (0,0) 除非你添加约束。
The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with design time attributes (such as layout_editor_absoluteX.) These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.
<?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
收到警告,我该怎么办?
转到设计,右键单击相对布局和select约束布局-->推断约束
出现这个问题是因为你有使用并且你的视图没有任何约束
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"
它仅在设计时(在 Android Studio 的预览版中)有效(这里的效果是您的视图将留出 8dp 的边距和 8dp 的顶部边距),在运行时它不会有任何影响
要解决此警告,您应该为布局添加足够的约束或简单地删除 tools:layout_editor_absoluteY="8dp"
和 tools:layout_editor_absoluteX="8dp"
拖动对象连接到墙壁...创建墙壁和对象之间的连接
看图例
如图所示制作 connection.In 约束布局,我们可以将小部件放置在任何地方,但 运行 在应用程序上,小部件与您在 android 开发期间放置的位置不同studio.To 修复此问题,通过从边缘拖动确保小部件具有水平和垂直约束 connections.Widgets 每一侧都有圆形的东西,拖动它以调整以固定其位置。
https://i.stack.imgur.com/r8L8O.png
当您的布局或小部件没有约束代码但 android 视图 needs.For 例如,当您使用 android:layout_marginTop="12dp" 时,您必须添加 app:layout_constraintTop_toTopOf="",如果没有出现警告。
你的相对布局没有约束,约束布局和相对布局都是独立的。转到 Design/Text 选项卡以正确定位。您也可以考虑仅使用两种布局中的一种;约束布局与相对布局一样有效
首先,由于您使用的是 ConstraintLayout,因此不需要在其中嵌入子 RelativeLayout。我的意思是,这就像同时驾驶两辆车一样,这是多余的。您应该仅使用 RelativeLayout 或仅使用 ConstraintLayout。
但是,如果您仍然坚持这种实现方式,那么实际问题就出在这里:
您在 RelativeLayout 中使用了 tools:layout_editor_absoluteX
和 tools: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 将在垂直方向(顶部和底部)和水平方向(开始和结束)受到约束。
希望对您有所帮助。编码愉快!