CardView中的Android ConstraintLayout问题

时间:2019-01-24 16:48:33

标签: android android-constraintlayout

我在android.support.constraint.ConstraintLayout内的android.support.v7.widget.CardView上遇到了问题

基本上我想嵌套ConstraintLayout

<android.support.constraint.ConstraintLayout>
  <android.support.v7.widget.CardView>
    <android.support.constraint.ConstraintLayout>
       <TextView/>
       <Spinner/>
       <android.support.design.widget.TextInputLayout>
         </EditText>
       <android.support.design.widget.TextInputLayout>
    </android.support.constraint.ConstraintLayout>
  </android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>

然后,我不知道为什么,第二个ConstraintLayout孩子没有出现。请在下面查看预览设计。

enter image description here

这是完整的代码

<?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=".FindTourFragment" android:id="@+id/frameLayout2"
    tools:layout_editor_absoluteY="25dp">

    <include
            layout="@layout/toolbar_main"
            android:id="@+id/job_list_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="4dp"
            app:layout_constraintTop_toBottomOf="@id/job_list_toolbar">

        <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:orientation="horizontal">
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Lokasi"
                    android:id="@+id/textView"
                    android:layout_marginTop="20dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginEnd="20dp"
                    style="@style/InputLabel"

                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="parent"/>

            <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:id="@+id/spinner"

                    android:textSize="18sp"
                    android:layout_marginStart="20dp"
                    android:layout_marginEnd="20dp"
                    android:layout_marginTop="15dp"

                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@id/textView"/>


            <android.support.design.widget.TextInputLayout
                    android:id="@+id/text_input_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"

                    android:layout_marginBottom="10dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginEnd="20dp"

                    android:textSize="18sp"
                    app:layout_constraintTop_toBottomOf="@+id/spinner"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent">

                <EditText
                        android:hint="Tanggal Keberangkatan"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="date"
                        android:ems="10"
                        android:id="@+id/editText2"
                        android:clickable="true"/>

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

    <ListView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/listview"

            app:layout_constraintTop_toBottomOf="@+id/card_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>

Ps。我也尝试使用模拟器,结果相同。谢谢

1 个答案:

答案 0 :(得分:0)

内部TextView中的ConstraintLayout具有以下属性:

app:layout_constraintTop_toBottomOf="parent"

这将您的TextView定位在ConstraintLayout的边界之外。由于其他所有内容均受限于此视图,因此其他所有内容均不在ConstraintLayout的范围之内。

更改为仅限于父级的顶部

app:layout_constraintTop_toTopOf="parent"