使用 CoordinatorLayout 时,我的 ScrollView 大小不正确
When using the CoordinatorLayout my ScrollView has an incorrect size
我在布局中使用 ScrollView,并尝试使用新的 CoordinatorLayout
from the design support library。
我的布局文件如下所示:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...
</LinearLayout>
</ScrollView>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar ... />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
测试时,滚动视图仅占据屏幕的一部分。出了什么问题?
标准的 ScrollView 只能用作父级。您需要将 ScrollView 更改为 android.support.v4.widget.NestedScrollView
.
可以在 AppBarLayout
的参考文档中看到示例。
NestedScrollView is just like ScrollView, but it supports acting as
both a nested scrolling parent and child on both new and old versions
of Android. Nested scrolling is enabled by default.
您可以在父 ScrollView 中使用 NestedScrollView。当需要在另一个滚动视图中滚动视图时,使用 NestedScrollView。当系统需要决定滚动哪个视图时,这就是有用的地方。
下面是带有 CoordinatorLayout 的 NestedScrollView 示例:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- Your scrolling content -->
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
...
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
...
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
我在布局中使用 ScrollView,并尝试使用新的 CoordinatorLayout
from the design support library。
我的布局文件如下所示:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...
</LinearLayout>
</ScrollView>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar ... />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
测试时,滚动视图仅占据屏幕的一部分。出了什么问题?
标准的 ScrollView 只能用作父级。您需要将 ScrollView 更改为 android.support.v4.widget.NestedScrollView
.
可以在 AppBarLayout
的参考文档中看到示例。
NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.
您可以在父 ScrollView 中使用 NestedScrollView。当需要在另一个滚动视图中滚动视图时,使用 NestedScrollView。当系统需要决定滚动哪个视图时,这就是有用的地方。
下面是带有 CoordinatorLayout 的 NestedScrollView 示例:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- Your scrolling content -->
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
...
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
...
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>