如何在NestedScrollView中显示滚动条

23

嘿,我在一个活动中实现了NestedScrollView,但是我无法像在ScrollView中那样显示滚动条,你们能帮我吗?

如何显示它?

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appBar">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:orientation="vertical"
            android:paddingLeft="@dimen/dimen_2"
            android:paddingRight="@dimen/dimen_2">
        </LinearLayout>
</android.support.v4.widget.NestedScrollView>

3
你已经知道答案了,你同时写下了问题和答案。请不要这样做! - rupinderjeet
3
@rupinderjeet 实际上,在stackoverflow上发布问题和答案是可以的。毕竟,stackoverflow的目标是为开发人员提供解答问题的资源。重要的不是谁提供了答案,而是答案是否正确。请注意,我的翻译尽力保留原意,同时让内容更加通俗易懂。 - Ridcully
@Ridcully 谢谢你。这些天我很天真。有时我还是会,我很抱歉。 - rupinderjeet
2个回答

49
使用android:scrollbars属性。
例如:
android:scrollbars="vertical"

android:scrollbars="horizontal"

android:scrollbars="vertical|horizontal"

例如:

<android.support.v4.widget.NestedScrollView
    android:id="@+id/foo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical">

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

文档链接:https://developer.android.com/reference/android/view/View.html#attr_android:scrollbars

这个链接是关于 Android 中的 View 属性 attr_android:scrollbars 的文档。它描述了如何在应用程序中添加滚动条,以便用户能够在屏幕上滚动视图。


6

我找到了解决方案,首先将NestedScrollView的行为设置为"@string/appbar_scrolling_view_behavior",然后我创建了一个样式,在所有需要滚动条的NestedScrollViews中显示它。

styles.xml中:

<resources>
    <!-- other styles -->

    <style name="NestedScrollBarStyle">
        <item name="android:scrollbarFadeDuration">2</item>
        <item name="android:scrollbars">vertical</item>
        <item name="android:fillViewport">true</item>
        <item name="android:orientation">vertical</item>
    </style>
</resources>

在布局中:

<android.support.v4.widget.NestedScrollView
    style="@style/NestedScrollBarStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/appBar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:orientation="vertical"
        android:paddingLeft="@dimen/dimen_2"
        android:paddingRight="@dimen/dimen_2">
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

1
谢谢,有两点需要注意:1. 你认为2毫秒是scrollbarFadeDuration的一个好的默认值吗?在Android 6.0上,ScrollView使用250毫秒(确认Log.d(TAG, "" + (new ScrollView(this)).getScrollBarFadeDuration());)。2. 你真的需要设置app:layout_behavior吗?在我的测试中,这个设置是无关紧要的。 - Simon Warta
我还可以确认,对于此操作,并不需要使用app:layout_behavior属性,而且250毫秒的淡入淡出时间更加自然。请参考我的答案,其中提供了一个更简单的解决方案。 - Michael Peterson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接