如何从自定义视图中访问layout_height?
我有一个自定义视图,我只想访问 layout_height
的 xml 布局值。
我目前正在获取该信息并将其存储在 onMeasure 期间,但这仅在首次绘制视图时发生。我的视图是 XY 图,它需要尽早知道其高度,以便可以开始执行计算。
该视图位于 viewFlipper 布局的第四页上,因此用户可能暂时不会翻转到它,但是当他们翻转到它时,我希望视图已经包含数据,这要求我有高度进行计算。
谢谢!!!
I have a custom view and I simply wish to access the xml layout value of layout_height
.
I am presently getting that information and storing it during onMeasure, but that only happens when the view is first painted. My view is an XY plot and it needs to know its height as early as possible so it can start performing calculations.
The view is on the fourth page of a viewFlipper layout, so the user may not flip to it for a while, but when they do flip to it, I would like the view to already contain data, which requires that I have the height to make the calculations.
Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那行得通:)...您需要将“android”更改为“http://schemas。 android.com/apk/res/android"
that work :)... you need to change "android" for "http://schemas.android.com/apk/res/android"
你可以使用这个:
You can use this:
来自 public View(Context context, AttributeSet attrs) 构造函数文档:
因此,为了实现您所需要的,请为您的自定义视图提供一个构造函数,该构造函数将 Attributes 作为参数,即:
From public View(Context context, AttributeSet attrs) constructor docs:
So to achieve what you need, provide a constructor to your custom view that takes Attributes as a parameter, i.e.:
•
Kotlin 版本
针对此问题编写的答案并未完全涵盖该问题。事实上,他们正在互相补足。总结一下答案,首先我们应该检查
getAttributeValue
返回值,然后如果layout_height
定义为维度值,则使用getDimensionPixelSize
检索它:•
Kotlin Version
The answers which are written to this question do not completely cover the issue. Actually, they are completing each other. To sum up the answer, first we should check the
getAttributeValue
returning value, then if thelayout_height
defined as dimension values, retrieve it usinggetDimensionPixelSize
: