2016-09-25 680 views
2

嘿,我在一个Activity中实现了一个NestedScrollView,但是我不能像在ScrollView中那样显示滚动条,可以吗?如何在NestedScrollView中显示滚动条

我该如何展示它?

<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> 
+1

你已经知道答案了,你写了这个任务离子并同时回答。请不要这样做! – rupinderjeet

回答

0

我找到了解决办法,首先设置NestedScrollView行为“@字符串/ 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'吗?在我的测试中,这个设置是无关紧要的。 –

+0

我也可以确认app不需要layout_behavior属性,250ms是一种更自然的淡入淡出时间。看到我的答案更简单的解决方案。 –

1

使用的android:滚动条属性。

如:

机器人:滚动条= “垂直”

机器人:滚动条= “水平”

机器人:滚动条= “垂直|水平”

<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