【问题标题】:Element LinearLayout is not allowed here - inside RelativeLayout此处不允许元素 LinearLayout - 在 RelativeLayout 内
【发布时间】:2020-03-15 01:55:49
【问题描述】:

我不断收到一个错误,即我的 LinearLayout 在我的 RelativeLayout 对象中是不允许的。 我正在尝试创建一个多过滤器搜索栏。此 LinearLayout 旨在在搜索栏中保存不同的元素。这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#212121"
        android:minHeight="?attr/actionBarSize">


        <TextView
            android:id="@+id/toolbar_title"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/app_name"
            android:textColor="#FFF" />


    </androidx.appcompat.widget.Toolbar>


    <RelativeLayout
        android:id="@+id/view_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#50000000"
        android:clickable="true"
        android:visibility="invisible">

        <ProgressBar
            android:id="@+id/marker_progress"
            style="?android:attr/progressBarStyle"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerInParent="true"
            android:indeterminate="true"
            android:visibility="gone" />
    </RelativeLayout>

    <ListView
        android:id="@+id/listContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:clipToPadding="false"
        android:divider="#fff"
        android:paddingTop="56dp"
        android:visibility="gone" />

    <androidx.appcompat.widget.CardView
        android:id="@+id/card_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:visibility="invisible"
        card_view:cardCornerRadius="2dp">

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

            <LinearLayout
                android:id="@+id/linearLayout_search"
                android:layout_width="match_parent"
                android:layout_height="48dp">

                <ImageView
                    android:id="@+id/image_search_back"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:background="?android:attr/selectableItemBackground"
                    android:clickable="true"
                    android:padding="12dp"
                    android:src="@mipmap/ic_arrow_back" />

                <EditText
                    android:id="@+id/edit_text_search"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#fff"
                    android:focusable="true"
                    android:gravity="center_vertical"
                    android:hint="@string/search_restaurants_and_cuisines"
                    android:imeOptions="actionSearch"
                    android:inputType="textCapWords"
                    android:maxLines="1"
                    android:paddingLeft="12dp"
                    android:paddingRight="8dp" />

            </LinearLayout>

            <View
                android:id="@+id/line_divider"
                android:layout_width="match_parent"
                android:layout_height=".5dp"
                android:layout_below="@+id/linearLayout_search"
                android:background="#eee" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_below="@+id/line_divider"
                android:divider="#FFFFFF" />
        </RelativeLayout>
    </androidx.appcompat.widget.CardView>


    <TextView
        android:id="@+id/txtNoResultsFound"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:padding="@dimen/activity_horizontal_margin"
        android:text="@string/no_results_found" />


    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:layout_marginBottom="@dimen/corners_small_value"
        android:layout_marginLeft="@dimen/corners_small_value"
        android:layout_marginRight="@dimen/corners_small_value">

    </ListView>

    <View
        android:id="@+id/toolbar_shadow"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_below="@+id/toolbar"
        android:background="@drawable/toolbar_shadow" />
</RelativeLayout>

为什么会这样?我确定它在更新的androidx CardView中,所以不知道为什么LinearLayout仍然不允许在CardView里面的RelativeLayout中。

【问题讨论】:

    标签: android android-layout android-linearlayout android-relativelayout


    【解决方案1】:

    您的CardView 标签中似乎有错字:

    <androidx.appcompat.widget.CardView
    

    应该是这样的:

    <androidx.cardview.widget.CardView ...
    

    (将.appcompat. 替换为.cardview.)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多