Bottomsheet Error:该视图不是CoordinatorLayout的子级

时间:2018-06-29 10:26:29

标签: android android-coordinatorlayout bottom-sheet

我想创建一个布局,其中80%的屏幕包含单独的布局,而20%的屏幕包含垂直的底部。但是,当我尝试执行此操作时,出现错误“视图不是CoordinatorLayout的子级”。我得到了错误,但我想不出其他方法来做到这一点。有人可以帮我实现它吗?帮助将不胜感激。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
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"
android:background="#efefef">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5">

<include
    layout="@layout/content_main_new"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4" />

<include
    layout="@layout/content_bottom_sheet2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

</LinearLayout>

</android.support.design.widget.CoordinatorLayout>

content_bottom_sheet2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomSheetLayout"
    android:layout_width="match_parent"
    android:layout_height="230dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    android:padding="@dimen/activity_vertical_margin"
    app:behavior_peekHeight="90dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <!--android:background="@android:color/holo_orange_light"-->

    <android.support.v7.widget.CardView
        android:id="@+id/cvBottomCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="@dimen/spIconsize"
        android:visibility="visible"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="10dp"
        app:cardElevation="5dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout

                android:id="@+id/linearLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:paddingTop="10dp"

                android:orientation="horizontal">

                <Button
                    android:layout_marginBottom="5dp"
                    android:id="@+id/btnReview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:layout_weight="1"
                    android:background="@drawable/rounded_button_darkgreen_bg"
                    android:text="Review"
                    android:textColor="@color/white" />

                <Button
                    android:layout_marginBottom="5dp"
                    android:id="@+id/btnReschedule"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="1"
                    android:background="@drawable/rounded_button_grey"
                    android:text="Reschedule"
                    android:textColor="@color/white" />
            </LinearLayout>


            <RelativeLayout
                android:id="@+id/relLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/linearLayout"
                android:layout_marginTop="5dp"
                android:padding="10dp"
                android:visibility="visible">

                <ImageButton
                    android:id="@+id/imageButton"
                    android:layout_width="80dp"
                    android:layout_height="90dp"
                    android:src="@drawable/user"
                    android:visibility="gone" />

                <TextView
                    android:id="@+id/tvappointmentId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toEndOf="@+id/imageButton"
                    android:text="Appointment ID - 5727"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/tvCustomerName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tvappointmentId"
                    android:layout_toEndOf="@+id/imageButton"
                    android:text="Name - Virat Sharma"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/tvLoanType"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tvCustomerName"
                    android:layout_toEndOf="@+id/imageButton"
                    android:text="Loan Type - Home LOAN" />

                <TextView
                    android:id="@+id/tvLoanAmount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tvLoanType"
                    android:layout_toEndOf="@+id/imageButton"
                    android:text="Loan Amount - 5727" />

                <TextView
                    android:id="@+id/tvTime"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/tvappointmentId"
                    android:layout_alignBottom="@+id/tvappointmentId"
                    android:layout_alignParentEnd="true"
                    android:gravity="right"
                    android:text="01:22 Mins"
                    android:textColor="@color/red_error"
                    android:textStyle="bold"
                    android:visibility="gone" />

            </RelativeLayout>

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

</LinearLayout>
</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:2)

可以请这样尝试吗?


<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomSheetLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:behavior_peekHeight="90dp"
    app:layout_behavior="@string/bottom_sheet_behavior">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="230dp"
        android:background="@color/colorPrimary"
        android:orientation="vertical"
        android:padding="@dimen/activity_vertical_margin">
             ...
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

我所要做的就是将稍后将要用作其底页的id放在根元素中(顺便说一下,如果需要,可以是ConstraintLayout),您会注意到,该根的父级元素将自动为CoodinatorLayout。 第二件事是将该元素设置为高度wrap_content 布局行为也应该在根元素中。 app:layout_behavior="@string/bottom_sheet_behavior"