向自定义 ViewGroup 添加滚动条?
我的 XML 布局中有以下节点:
<com.x.y.view.TagLayout android:id="@+id/TagLayout"
android:layout_width="fill_parent" android:layout_height="170dip" />
TagLayout 是一个扩展 ViewGroup 的类,基本上包含一堆带有文本的按钮(充当标签的自定义对象,但它既不在这里也不在那里)。
问题是当按钮太多时,视图不再滚动。我可以确认我正在添加标签,但我看不到任何内容。
如果我将上述内容包装在 ScrollView 中,则布局永远不会呈现。我尝试添加属性 android:isScrollContainer 和 android:scrollbars,但这不会改变任何内容。我在这里缺少什么吗?
我还应该补充一点,TagLayout 正在重写 onMeasure() 事件。我想我需要在那里实现滚动机制......?
I've got the following node in my XML layout:
<com.x.y.view.TagLayout android:id="@+id/TagLayout"
android:layout_width="fill_parent" android:layout_height="170dip" />
TagLayout is a class that extends ViewGroup, and basically holds a bunch of buttons with text (a custom object that act as tags, but that's neither here or there).
The problem is that when there are too many buttons, the view no longer scrolls. I can confirm that I am adding tags but I can't see anything.
If I wrap the above within a ScrollView
, the layout never renders. I tried adding the attributes android:isScrollContainer and android:scrollbars, but that doesn't change anything. Is there something I'm missing here?
I should also add that TagLayout is overriding the onMeasure() event. I guess I need to implement a scrolling mechanism there...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,我基本上明白了。我实际上必须用 LinearLayout 包裹上面的节点,然后将其包裹在 ScrollView 周围。填充有点偏离(最后一个项目从视图中删除),但它处于正确的轨道上。
Well, I mostly got it. I had to actually wrap the node above with a LinearLayout, then wrap THAT around a ScrollView. The padding is a little off (the last item is cut from the view) but it's on the right track.
您可以向自定义
ViewGroup
添加滚动功能,而无需将其包装在多个其他布局和视图中(事实上,如果不需要,最好不要包装它......拥有平面布局层次结构非常重要更好的性能)。只需执行此问题中描述的所有操作,并确保将setWillNotDraw(false);
添加到您的自定义ViewGroup
构造函数,如本答案中所述。You can add scrolling to your custom
ViewGroup
without wrapping it in multiple other layouts and views (in fact, it's better not to wrap it if you don't have to...having flat layout hierarchies is much better for performance). Just do everything described in this question, and make sure you addsetWillNotDraw(false);
to your customViewGroup
constructor as described in this answer.