如何在 Android 中向 LinearLayout 添加滚动条?
如何向 Android 视图添加滚动条?
我尝试将 android:scrollbars:"vertical"
添加到布局 XML 文件中的 LinearLayout
中,但它不起作用。
我以为滚动条是在 Android 中默认绘制的,但似乎并非如此。看来我们必须自己画它——我该怎么做呢?
How do I add scrollbars to a view in Android?
I tried by adding android:scrollbars:"vertical"
to the LinearLayout
in my layout XML file but it is not working.
I thought scrollbars were drawn by default in Android but it does not seem that way. It seems we have to draw it ourselves - how do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法将滚动条添加到 LinearLayout,因为它不是可滚动容器。
仅显示可滚动容器,例如
ScrollView
、HorizontalScrollView
、ListView
、GridView
、ExpandableListView
滚动条。我建议您将
LinearLayout
放置在ScrollView
中,如果有足够的内容可以滚动,默认情况下它将显示垂直滚动条。如果您希望始终显示垂直滚动条,请将
android:scrollbarAlwaysDrawVerticalTrack="true"
添加到ScrollView
中。请注意,LinearLayout
的高度设置为wrap_content
- 这意味着LinearLayout
的高度可以大于ScrollView 的高度
如果有足够的内容 - 在这种情况下,您将能够上下滚动LinearLayout
。You cannot add scrollbars to a
LinearLayout
because it is not a scrollable container.Only scrollable containers such as
ScrollView
,HorizontalScrollView
,ListView
,GridView
,ExpandableListView
show scrollbars.I suggest you place your
LinearLayout
inside aScrollView
which will by default show vertical scrollbars if there is enough content to scroll.If you want the vertical scrollbar to always be shown, then add
android:scrollbarAlwaysDrawVerticalTrack="true"
to yourScrollView
. Note the height of theLinearLayout
is set towrap_content
- this means the height of theLinearLayout
can be larger than that of theScrollView
if there is enough content - in that case you will be able to scroll yourLinearLayout
up and down.您无法以这种方式向小部件添加滚动条。您可以将小部件包装在
ScrollView
中。这是一个简单的例子:如果你想用代码来做:
You can't add a scrollbar to a widget that way. You can wrap your widget inside a
ScrollView
. Here's a simple example:If you want to do it in code: