我有一个需要滚动的布局。这是一个片段,片段托管在一个活动上。ScrollView 只有一个孩子,它是 relativeLayout 而 relativeLayout 有多个孩子。但我仍然得到错误。错误发生在我安装并打开屏幕,5 或 6 秒后,应用程序崩溃了。 这是我的片段布局结构:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/settingsAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
/>
<com.google.android.material.button.MaterialButton
android:id="@+id/backButton"
style="@style/back_arrow_button"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.cardview.widget.CardView
>
<LinearLayout
>
<RelativeLayout style="@style/settings_relative_layout_style">
<TextView
/>
<com.google.android.material.imageview.ShapeableImageView
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
/>
<TextView
style="@style/settings_text_style"
/>
<RelativeLayout style="@style/settings_relative_layout_style">
<TextView
/>
<ImageView
/>
</RelativeLayout>
<RelativeLayout style="@style/settings_relative_layout_style">
<TextView
/>
<ImageView
/>
</RelativeLayout>
<View
android:layout_height="1dp"
/>
<TextView
/>
<RelativeLayout style="@style/settings_relative_layout_style">
<TextView
/>
<ImageView
/>
</RelativeLayout>
<RelativeLayout style="@style/settings_relative_layout_style">
<TextView
/>
<ImageView
/>
</RelativeLayout>
<RelativeLayout style="@style/settings_relative_layout_style">
<TextView
/>
<ImageView
/>
</RelativeLayout>
<RelativeLayout style="@style/settings_relative_layout_style">
<TextView
/>
<ImageView
style="@style/settings_image_view_style"
/>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
</ScrollView>
</layout>
这是主要活动的布局:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.presentations.activity.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomMenu"
app:defaultNavHost="true" />
<me.ibrahimsn.lib.SmoothBottomBar
android:visibility="gone"
app:iconSize="@dimen/l"
android:elevation="@dimen/xxxs"
app:iconTintActive="@color/nav_icon_color"
app:iconTint="#D0D0D0"
android:id="@+id/bottomMenu"
android:layout_width="match_parent"
android:layout_height="@dimen/xxxxxxl"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav_menu" />
</RelativeLayout>
</layout>
错误消息:
FATAL EXCEPTION: main Process: ltd.tinkers.example, PID: 10147 java.lang.IllegalStateException: ScrollView can host only one direct child at androidx.core.widget.NestedScrollView.addView(NestedScrollView.java:515) at com.google.android.material.snackbar.BaseTransientBottomBar$SnackbarBaseLayout.addToTargetParent(BaseTransientBottomBar.java:1330) at com.google.android.material.snackbar.BaseTransientBottomBar.showView(BaseTransientBottomBar.java:757) at com.google.android.material.snackbar.BaseTransientBottomBar$1.handleMessage(BaseTransientBottomBar.java:263) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7948) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
我尝试使用 NestedScrollView 但问题没有解决。我用从堆栈溢出答案中找到的 FrameLayout 包装片段布局滚动视图。但这没有用。