宽度设置为“match_constraint”时,ConstraintLayout消失

时间:2018-02-02 11:33:46

标签: android android-constraintlayout

我正在制作CardView三个ConstraintLayouts。一个ConstraintLayout包裹两个并排放置的ConstraintLayout。 问题是如果宽度设置为ConstraintLayout,则右侧match_constraint消失。它似乎在Android Studio中很好,但是当我在手机上运行它时,它就消失了。我假设“match_constraint”的原因将宽度设​​置为0dp。我希望右侧约束根据手机动态改变宽度。 以下是我的布局XML文件。

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

    <android.support.v7.widget.CardView
        android:id="@+id/cv_result_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:padding="8dp"
        card_view:cardBackgroundColor="#eee"
        card_view:cardCornerRadius="5dp">

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

            <android.support.constraint.ConstraintLayout
                android:id="@+id/cl_left-panel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:background="@color/blank_color"
                card_view:layout_constraintBottom_toBottomOf="parent"
                card_view:layout_constraintStart_toStartOf="parent"
                card_view:layout_constraintTop_toTopOf="parent">

                <ImageView
                    android:id="@+id/iv_result_quiz_detail"
                    android:layout_width="140dp"
                    android:layout_height="115dp"
                    android:layout_margin="4dp"
                    android:contentDescription="@string/iv_result_quiz_detail"
                    card_view:layout_constraintEnd_toEndOf="parent"
                    card_view:layout_constraintTop_toTopOf="parent"
                    card_view:srcCompat="@mipmap/ic_launcher" />

                <TextView
                    android:id="@+id/tv_no_of_likes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/iv_result_quiz_detail"
                    android:layout_marginStart="4dp"
                    android:layout_marginTop="4dp"
                    android:layout_toEndOf="@+id/chb_like"
                    android:gravity="top"
                    android:text="@string/tv_no_of_likes"
                    android:textSize="12sp"
                    android:textStyle="italic"
                    card_view:layout_constraintStart_toEndOf="@+id/chb_like"
                    card_view:layout_constraintTop_toBottomOf="@+id/iv_result_quiz_detail" />

                <CheckBox
                    android:id="@+id/chb_like"
                    android:layout_width="17dp"
                    android:layout_height="17dp"
                    android:layout_below="@+id/iv_result_quiz_detail"
                    android:layout_margin="4dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="8dp"
                    android:background="@drawable/selector_like"
                    android:button="@null"
                    android:contentDescription="@string/ib_like"
                    android:scaleType="fitXY"
                    card_view:layout_constraintBottom_toBottomOf="parent"
                    card_view:layout_constraintStart_toStartOf="@+id/iv_result_quiz_detail"
                    card_view:layout_constraintTop_toBottomOf="@+id/iv_result_quiz_detail" />
            </android.support.constraint.ConstraintLayout>

            <android.support.constraint.ConstraintLayout
                android:id="@+id/cl_right_panel"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:background="@color/blank_color"
                card_view:layout_constraintBottom_toBottomOf="@+id/cl_left-panel"
                card_view:layout_constraintEnd_toEndOf="parent"
                card_view:layout_constraintStart_toEndOf="@+id/cl_left-panel"
                card_view:layout_constraintTop_toTopOf="@+id/cl_left-panel">

                <TextView
                    android:id="@+id/tv_quiz_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="16dp"
                    android:maxLines="2"
                    android:text="@string/test_question"
                    android:textSize="12sp"
                    android:textStyle="bold"
                    card_view:layout_constraintStart_toStartOf="parent"
                    card_view:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/tv_choice_01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_quiz_desc"
                    android:layout_marginStart="4dp"
                    android:layout_marginTop="8dp"
                    android:background="@drawable/shape_round_button_mint"
                    android:ellipsize="end"
                    android:gravity="start"
                    android:maxLines="1"
                    android:text="@string/test_choice"
                    android:textSize="12sp"
                    card_view:layout_constraintStart_toStartOf="@+id/tv_quiz_desc"
                    card_view:layout_constraintTop_toBottomOf="@+id/tv_quiz_desc" />

                <TextView
                    android:id="@+id/tv_choice_02"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_choice_01"
                    android:layout_marginStart="4dp"
                    android:layout_marginTop="4dp"
                    android:ellipsize="end"
                    android:gravity="start"
                    android:maxLines="1"
                    android:text="@string/test_choice"
                    android:textSize="12sp"
                    card_view:layout_constraintStart_toStartOf="@+id/tv_quiz_desc"
                    card_view:layout_constraintTop_toBottomOf="@+id/tv_choice_01" />

                <TextView
                    android:id="@+id/tv_choice_03"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_choice_02"
                    android:layout_marginStart="4dp"
                    android:layout_marginTop="4dp"
                    android:ellipsize="end"
                    android:gravity="start"
                    android:maxLines="1"
                    android:text="@string/test_choice"
                    android:textSize="12sp"
                    card_view:layout_constraintStart_toStartOf="@+id/tv_quiz_desc"
                    card_view:layout_constraintTop_toBottomOf="@+id/tv_choice_02" />

                <TextView
                    android:id="@+id/tv_choice_04"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_choice_03"
                    android:layout_marginStart="4dp"
                    android:layout_marginTop="4dp"
                    android:ellipsize="end"
                    android:gravity="start"
                    android:maxLines="1"
                    android:text="@string/test_choice"
                    android:textSize="12sp"
                    card_view:layout_constraintStart_toStartOf="@+id/tv_quiz_desc"
                    card_view:layout_constraintTop_toBottomOf="@+id/tv_choice_03" />
            </android.support.constraint.ConstraintLayout>

        </android.support.constraint.ConstraintLayout>

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

我的期望是什么 this

1 个答案:

答案 0 :(得分:0)

也许你的第一个ConstraintLayout(有固定宽度)占据了整个屏幕 尝试使用layout_constraintHorizontal_weight确定每个布局占用的屏幕部分。

左侧布局:

    <android.support.constraint.ConstraintLayout
        android:id="@+id/cl_left-panel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/blank_color"
        card_view:layout_constraintBottom_toBottomOf="parent"
        card_view:layout_constraintEnd_toStartOf="@id/cl_right_panel"
        card_view:layout_constraintHorizontal_weight="1"
        card_view:layout_constraintStart_toStartOf="parent"
        card_view:layout_constraintTop_toTopOf="parent">

正确的布局:

    <android.support.constraint.ConstraintLayout
        android:id="@+id/cl_right_panel"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="@color/blank_color"
        card_view:layout_constraintBottom_toBottomOf="@+id/cl_left-panel"
        card_view:layout_constraintEnd_toEndOf="parent"
        card_view:layout_constraintHorizontal_weight="1.5"
        card_view:layout_constraintStart_toEndOf="@+id/cl_left-panel"
        card_view:layout_constraintTop_toTopOf="@+id/cl_left-panel">

注意:您不需要根LinearLayout