嵌套的scrollView无法识别工具栏

时间:2016-05-30 14:31:14

标签: android xml android-layout android-studio scrollview

我的项目只有一个工具栏,它被AppBarLayout包围,下面还有一个NestedScrollView控件,它有一个带有CardView布局的线性布局。没有问题是嵌套滚动与我在图像中显示的工具栏重叠 enter image description here

并且在运行时它通过应用程序就像没有这样的工具栏:

enter image description here

enter image description here

这是我的代码:

double

scroll_view_item代码在这里:

<?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/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
   <android.support.design.widget.AppBarLayout
      android:id="@+id/appbarLayout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:fitsSystemWindows="true"
      >



<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="7dp"
    app:theme="@style/ThemeOverlay.AppCompat"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"

    android:titleTextColor="@color/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    ></android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="10dp"

            >
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

3 个答案:

答案 0 :(得分:6)

只需使用linearlayout包围您的代码:

<?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/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
   <android.support.design.widget.AppBarLayout
      android:id="@+id/appbarLayout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:fitsSystemWindows="true"
      >



<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="7dp"
    app:theme="@style/ThemeOverlay.AppCompat"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"

    android:titleTextColor="@color/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    ></android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="10dp"

            >
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

答案 1 :(得分:4)

您忘记在ScrollView上指定行为。

将此行添加到NestedScrollView:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

答案 2 :(得分:2)

将以下行添加到NestedScrollView

app:layout_behavior="@string/appbar_scrolling_view_behavior"