向自定义 ViewGroup 添加滚动条?

发布于 2024-09-12 13:43:50 字数 494 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我还不会笑 2024-09-19 13:43:50

嗯,我基本上明白了。我实际上必须用 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.

虐人心 2024-09-19 13:43:50

您可以向自定义 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 add setWillNotDraw(false); to your custom ViewGroup constructor as described in this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文