我有一个自定义视图,我只想访问layout_height的xml布局值.
我目前正在获取该信息并将其存储在onMeasure中,但这仅在首次绘制视图时才会发生.我的视图是XY图,它需要尽早知道它的高度,以便它可以开始执行计算.
视图位于viewFlipper布局的第四页上,因此用户可能暂时不会翻转它,但是当它们翻转到它时,我希望视图已经包含数据,这要求我有高度到进行计算.
谢谢!!!
解决方法
从公共视图(Context context,AttributeSet attrs)构造函数docs:
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作为参数,即:
public CustomView(final Context context,AttributeSet attrs) { super(context,attrs); String height = attrs.getAttributeValue("android","layout_height"); //further logic of your choice.. }