自定义 ViewGroup 示例?
我在这里搜索过,在谷歌上,在android文档上...
但是我找不到带有自定义视图组示例的代码片段,我最多找到一些模糊的解释...
有人可以提供一个吗?如何创建一个视图组,以便将其子项放置在您想要的位置?
I searched here on SO, on Google, on the android docs...
But I cannot find a single snippet of code with a example of custom viewgroup, I find at most some vague explanations...
Can someone provide one? How you make a viewgroup where you can put its children where you want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最简单的例子是 AbsoluteLayout.java
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/AbsoluteLayout.java
您需要重写 onMeasure 来测量子级,并重写 onLayout 来定位它们。
如果您愿意,我也可以分享更加复杂的 ViewGroup 代码。
I think the simplest example to look at is the source for AbsoluteLayout.java
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/AbsoluteLayout.java
You need to override onMeasure to measure the children and onLayout to position them.
I have strikingly more complicated ViewGroup code I can share as well if you want.
这并不简单,您需要做的就是在计算出视图的精确尺寸后调用
super.onMeasure
。It't quite simple, all you need to do is to call
super.onMeasure
after calculate the exact dimentions of yout view.