Java 将视图放置在ConstraintLayout中的另一个视图下

Java 将视图放置在ConstraintLayout中的另一个视图下,java,android,android-constraintlayout,Java,Android,Android Constraintlayout,如何将视图放置在约束视图中另一个视图的后面?基本上,我想做到这一点: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View andr

如何将
视图
放置在
约束视图
中另一个
视图
的后面?基本上,我想做到这一点:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<View
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<View
    android:id="@+id/view2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</FrameLayout>

当view1在Z轴上位于view2后面时。

只需尝试

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="VIEW 1"/>

    <TextView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="VIEW 2"
        app:layout_constraintTop_toTopOf="@+id/view1"
        app:layout_constraintStart_toStartOf="@+id/view1"
        app:layout_constraintEnd_toEndOf="@+id/view1"
        app:layout_constraintBottom_toBottomOf="@+id/view1"/>
</android.support.constraint.ConstraintLayout>

要指导
约束的子项layout
您可以使用大量
app:layout\u constraint…
属性来实现您所寻找的最佳布局

所有4个
layout\u约束
都是不必要的,其中任何一个都可以做到。但我只是想告诉你你的选择

试试看

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="VIEW 1"/>

    <TextView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="VIEW 2"
        app:layout_constraintTop_toTopOf="@+id/view1"
        app:layout_constraintStart_toStartOf="@+id/view1"
        app:layout_constraintEnd_toEndOf="@+id/view1"
        app:layout_constraintBottom_toBottomOf="@+id/view1"/>
</android.support.constraint.ConstraintLayout>

要指导
约束的子项layout
您可以使用大量
app:layout\u constraint…
属性来实现您所寻找的最佳布局


所有4个
layout\u约束
都是不必要的,其中任何一个都可以做到。但我只是想告诉你你的选择

此代码将把一个视图放在另一个视图下面

使用约束将视图约束到底部和顶部。 如果希望视图占据整个空间,请使用layout\u height=“0dp”和layout\u constraintHeight\u default=“spread”


此代码将在另一个视图下放置一个视图

使用约束将视图约束到底部和顶部。 如果希望视图占据整个空间,请使用layout\u height=“0dp”和layout\u constraintHeight\u default=“spread”


实际上与框架布局相同。 让我们一起来看看:

Xml:


上图具有以下xml布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="TextView2" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="TextView1" />
</android.support.constraint.ConstraintLayout>


正如您所看到的,只有顺序改变了。我希望这会有所帮助。

实际上与框架布局相同。 让我们一起来看看:

Xml:


上图具有以下xml布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="TextView2" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="TextView1" />
</android.support.constraint.ConstraintLayout>


正如您所看到的,只有顺序改变了。我希望这会有所帮助。

添加
app:layout\u constrainttoop\u toBottomOf=“@+id/view1”
将在视图的顶部和底部之间创建约束,而不会将它们彼此重叠。这不是我想要实现的。当你说把它们一个放在另一个上面时,你的意思是你想让view1垂直地在View2上面,还是像它们重叠一样在上面@benI看到可能您希望它们彼此重叠,我将相应地更新代码。添加
app:layout\u constraintTop\u toBottomOf=“@+id/view1”
将在视图的顶部和底部之间创建约束,而不会将它们彼此重叠。这不是我想要实现的。当你说把它们一个放在另一个上面时,你的意思是你想让view1垂直地在View2上面,还是像它们重叠一样在上面@benI,也许你希望它们彼此重叠,我会相应地更新代码。