如何在Android中向视图添加滚动条?
我尝试将android:scrollbars:"vertical"
添加到布局XML文件中的LinearLayout
中,但它不起作用。
我以为滚动条在安卓系统中是默认绘制的,但似乎不是这样。似乎我们必须自己画出来-我该怎么做?
最佳答案
不能将滚动条添加到LinearLayout
中,因为它不是可滚动容器。
仅可滚动容器,如ScrollView
、HorizontalScrollView
、ListView
、GridView
、ExpandableListView
显示滚动条。
我建议您将您的LinearLayout
放在ScrollView
中,如果有足够的内容滚动,默认情况下会显示垂直滚动条。
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<!-- Your content goes here -->
</LinearLayout>
</ScrollView>
如果希望始终显示垂直滚动条,请将
android:scrollbarAlwaysDrawVerticalTrack="true"
添加到您的ScrollView
中。注意,LinearLayout
的高度被设置为wrap_content
——这意味着,如果有足够的内容,LinearLayout
的高度可以大于ScrollView
的高度——在这种情况下,您可以上下滚动您的LinearLayout
。