【问题标题】:Bottomsheet Error : The view is not a child of CoordinatorLayout底页错误:视图不是 CoordinatorLayout 的子视图
【发布时间】:2018-06-29 10:26:28
【问题描述】:

我想创建一个布局,其中 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>

【问题讨论】:

  • content_main_new 的父布局是什么?
  • content_main_new.xml 的父级是线性布局
  • 在你的包含布局中使用 CoordinatorLayout 并尝试
  • 可以添加 content_bottom_sheet2 布局吗?
  • 查看我更新的帖子@GobuCSG

标签: android android-coordinatorlayout bottom-sheet


【解决方案1】:

请你试试这个方法好吗?


<?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),您可以注意到,这个根的父级element 将自动成为 CoodinatorLayout。 第二件事是把这个元素放在一个高度wrap_content 布局行为也应该在根元素中。 app:layout_behavior="@string/bottom_sheet_behavior"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-10
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 2017-06-14
    • 2012-06-01
    • 2016-06-21
    相关资源
    最近更新 更多