如何在Android中向视图添加滚动条?
我尝试将android:scrollbars:"vertical"添加到布局XML文件中的LinearLayout中,但它不起作用。
我以为滚动条在安卓系统中是默认绘制的,但似乎不是这样。似乎我们必须自己画出来-我该怎么做?

最佳答案

不能将滚动条添加到LinearLayout中,因为它不是可滚动容器。
仅可滚动容器,如ScrollViewHorizontalScrollViewListViewGridViewExpandableListView显示滚动条。
我建议您将您的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

07-24 09:49
查看更多