【问题标题】:how to solve this scroll view error "ScrollView can host only one direct child"如何解决此滚动视图错误“ScrollView can host only one direct child”
【发布时间】:2021-04-21 19:01:47
【问题描述】:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ECEFF1"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="end">

<androidx.core.widget.NestedScrollView
    android:layout_width="match parent"
    android:layout_height="match parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="65dp"
                android:background="#ED3733">

                <ImageView
                    android:id="@+id/homeScreenNavigation"
                    android:layout_width="40dp"
                    android:layout_height="60dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    android:src="@drawable/ic_baseline_menu_24" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@mipmap/ic_launcher_round" />

            </RelativeLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="30dp"
                android:text="Dashboard"
                android:textSize="40sp" />

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="280dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="16dp"
                android:layout_marginRight="10dp"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp">

            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="280dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="16dp"
                android:layout_marginRight="10dp"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp">

            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="280dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="16dp"
                android:layout_marginRight="10dp"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp">

            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="280dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="16dp"
                android:layout_marginRight="10dp"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp">

            </androidx.cardview.widget.CardView>
        </LinearLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start|left"
            app:menu="@menu/drawer_menu" />


    </androidx.core.widget.NestedScrollView>
</androidx.drawerlayout.widget.DrawerLayout>

此处“ScrollView 只能承载一个直接子级” 我不知道如何解决这个错误有人可以帮我解决这个问题 谢谢.................................,.............. ..................................................... ..................................................... ....................

【问题讨论】:

  • 正如错误所说,滚动视图应该只有一个直接子级,但有一个LinearLayout,然后是NavigationView。将NavigationView 移到LinearLayout 内或移出ScrollView

标签: java android xml android-recyclerview scrollview


【解决方案1】:

ScrollView 和 NestedScrollView 只能有一个孩子 您可以在 ScrollView 或 NestedScrollView 中使用 ConstraintLayout 或其他 ViewGroup。

祝你好运。

【讨论】:

    猜你喜欢
    • 2020-02-10
    • 2017-02-17
    • 2020-05-14
    • 2022-11-16
    • 2019-03-04
    • 1970-01-01
    • 2022-09-27
    • 2021-12-22
    • 2021-02-02
    相关资源
    最近更新 更多