首页 > 解决方案 > 滚动视图没有完全滚动

问题描述

在我的主要活动中,有导航抽屉和 fab 按钮以及 content.xml 内部,这是在我的主要活动中加载内容的主要 xml。换句话说,我有 android studio 提供的导航抽屉活动。

该 content.xml 中的代码是:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:fillViewport="true">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/b1"
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#f0eb79"
                android:foreground="?attr/selectableItemBackground"
                android:drawableTop="@drawable/computer"
                android:paddingTop="25dp"
                android:text="Chemistry"
                android:textAllCaps="false"
                android:textColor="#2f2e2e"
                android:textSize="18sp" />


            <Button
                android:id="@+id/b2"
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#524d47"
                android:foreground="?attr/selectableItemBackground"
                android:drawableTop="@drawable/computer"
                android:paddingTop="25dp"
                android:text="Sports"
                android:textAllCaps="false"
                android:textColor="#ffffff"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/b3"
                android:layout_width="0dp"
                android:foreground="?attr/selectableItemBackground"
                android:layout_height="200dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#e7595d"
                android:drawableLeft="@drawable/computer"
                android:paddingLeft="45dp"
                android:text="Inventions"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="18sp" />

        </LinearLayout>
    </LinearLayout>
</ScrollView>

如下图所示,它并没有完全滚动。我还在另一个没有 fab 和 nagivation 抽屉的空活动中测试了相同的 xml 代码,并且它完美地显示了滚动行为。

在此处输入图像描述

同样,这是我的 activitymain.xml 的代码:

<?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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

标签: androidxmlandroid-studioscrollview

解决方案


在滚动视图中添加这些属性后,它对我有用:

app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.example.nepali_test.MainActivity" 
tools:showIn="@layout/app_bar_main"

推荐阅读