Android:constraintLayout 在 CardView 中不起作用

Abh*_*hay 5 android android-button android-studio android-cardview android-constraintlayout

我正在尝试具有以下一般布局层次结构 ConstraintLayout>CardView>ConstraintLayout>Button 的布局。

第二个constraintLayout 必须贴在cardView 的右下角。

预期结果: 预期结果

但是卡片视图中的约束不起作用。

我首先尝试用 LinearLayout 替换 2nd ConstraintLayout 但它也没有帮助。约束对他们没有影响。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".home_fragment"
android:id="@+id/home_fragment">
<android.support.v7.widget.CardView
    android:id="@+id/first_card"
    android:layout_width="200dp"
    android:layout_height="100dp"
    app:cardBackgroundColor="@color/card_color"
    app:cardCornerRadius="15dp"
    app:layout_constraintTop_toTopOf="parent"

    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="200dp"
    >

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="Button 1 "
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    android:id="@+id/second_card"
    android:layout_width="200dp"
    android:layout_height="100dp"

    android:layout_marginTop="56dp"
    app:cardBackgroundColor="@color/card_color"
    app:cardCornerRadius="20dp"
    app:layout_constraintStart_toStartOf="@id/first_card"
    app:layout_constraintTop_toBottomOf="@id/first_card">

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="Button 2 "
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

    </android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

但是这里第二个 ConstraintLayout 被粘贴到 CardView 的左上角。我希望它粘贴在 CardView 的右下角。实际结果:

在此处输入图片说明

Sau*_*rat 3

像这样设置你的内部 ConstraintLayout :

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1 "
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)