Android:约束布局在CardView中不起作用

时间:2019-02-05 05:27:24

标签: android android-studio android-button android-cardview android-constraintlayout

我正在尝试具有以下常规布局层次结构的布局 ConstraintLayout> CardView> ConstraintLayout>按钮。

第二个约束布局必须粘贴到cardView的右下角。

预期结果: Expected Result

但是Card View内的约束不起作用。

我首先尝试用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>

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

enter image description here

9 个答案:

答案 0 :(得分:2)

像这样设置内部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>

答案 1 :(得分:1)

您不能将约束应用于您的Cardview子级,android:layout_gravity="bottom|right"应用于ConstraintLayout内的CardView

答案 2 :(得分:1)

您必须像下面那样制作内部布局match_parent,然后才能轻松实现constraints

<?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"
    tools:context=".home_fragment"
    android:layout_height="match_parent"
    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="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:text="Button 1 "
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="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="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            >

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

                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:text="Button 2 "
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

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

</android.support.constraint.ConstraintLayout>

答案 3 :(得分:1)

尝试这个

<?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="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent">

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

            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:text="Button 1 "
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="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="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        >

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

            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:text="Button 2 "
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

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

答案 4 :(得分:1)

您在cardview内部的约束布局应具有宽度和高度match_parent。否则约束布局将缩小以匹配其子级宽度和高度。

    <?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 = "match_parent" // change this line
        android:layout_height = "match_parent" // change this line
        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_constraintRight_toRightOf = "parent"
            app:layout_constraintBottom_toBottomOf = "parent"
            android:layout_marginBottom = "8dp" />

    </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 = "match_parent" // change this line
        android:layout_height = "match_parent" // change this line
        app:layout_constraintRight_toRightOf = "parent"
        app:layout_constraintBottom_toBottomOf = "parent"
        >

        <Button
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            app:layout_constraintRight_toRightOf = "parent"
            app:layout_constraintBottom_toBottomOf = "parent"
            android:text = "Button 2 "
            />

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

答案 5 :(得分:1)

要达到预期的效果,您无需在CardView内使用约束布局:

您可以在不使用CardView内部约束的情况下做到这一点,

只需将android:layout_gravity="bottom|end"应用于CardView内的按钮

在下面检查更新的代码和屏幕截图:

Screenshot

<?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"
    android:id="@+id/home_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/first_card"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="200dp"
        app:cardCornerRadius="15dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:text="Button 1 " />

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

    <android.support.v7.widget.CardView
        android:id="@+id/second_card"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_margin="10dp"
        android:layout_marginTop="56dp"
        app:cardCornerRadius="20dp"
        app:layout_constraintStart_toStartOf="@id/first_card"
        app:layout_constraintTop_toBottomOf="@id/first_card">

        <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:layout_gravity="bottom|end"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:text="Button 2 "
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:text="Button 2 "
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

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

</android.support.constraint.ConstraintLayout>

答案 6 :(得分:1)

  

检查

输出:enter image description here

const [ pendingMessages, setPendingMessages ] = React.useState([]);

React.useEffect(function() {
    ref.current.addEventListener('send-message', onSendMessage);
    return function() {
      ref.current.removeEventListener('send-message', onSendMessage);
    };
  }, []);

function onSendMessage(event) {
  const newMessage = event.message;
  setPendingMessages(prevState =>([ ...prevState, newMessage ]));
}

答案 7 :(得分:1)

尝试使用此代码在约束按钮右侧设置按钮:

  <android.support.v7.widget.CardView
    android:id="@+id/first_card"
    android:layout_width="200dp"
    android:layout_height="100dp"
    app:cardBackgroundColor="#3F51B5"
    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="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/button_1">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1 "
            app:layout_constraintEnd_toEndOf="@id/button_1"
           app:layout_constraintBottom_toBottomOf="@+id/button_1" />

    </android.support.constraint.ConstraintLayout>

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

屏幕截图: enter image description here

答案 8 :(得分:0)

authenticateUser