如何从自定义视图中访问layout_height? [英] How do I access layout_height from within my custom view?

查看:147
本文介绍了如何从自定义视图中访问layout_height?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义视图,我只是希望访问layout_height的xml布局值.

I have a custom view and I simply wish to access the xml layout value of layout_height.

我目前正在获取该信息并将其存储在onMeasure期间,但这仅在第一次绘制视图时发生.我的视图是XY图,它需要尽早知道其高度,以便开始执行计算.

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.

该视图位于viewFlipper布局的第四页上,因此用户可能暂时不会翻转至该页面,但是当他们翻转至该页面时,我希望该视图已经包含数据,这要求我拥有进行计算的高度.

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.

谢谢!

推荐答案

公共视图(上下文上下文,AttributeSet属性)构造函数文档:

在以下情况下调用的构造函数 从XML扩展视图.这是 在创建视图时调用 从XML文件构造而成, 提供的属性是 在XML文件中指定.

Constructor that is called when inflating a view from XML. This is called when a view is being constructed from an XML file, supplying attributes that were specified in the XML file.

因此,要实现所需的功能,请向您的自定义视图提供一个构造函数,该构造函数将Attributes作为参数,即:

So to achieve what you need, provide a constructor to your custom view that takes Attributes as a parameter, i.e.:

public CustomView(final Context context, AttributeSet attrs) {
    super(context, attrs);
    String height = attrs.getAttributeValue("android", "layout_height");
    //further logic of your choice..
}

这篇关于如何从自定义视图中访问layout_height?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆