简体   繁体   English

仅覆盖一个组件 - Flex Mobile,覆盖获取宽度或高度

[英]Override get width or height for just one component- Flex Mobile

Just a little background on what I am trying to do. 关于我想要做的事情的一点背景。 I have a custom skin where I have a stylable text field to display the date. 我有一个自定义皮肤,我有一个可设置的文本字段来显示日期。 When clicking on the stylable text field, which is binded to the date, a date spinner comes up. 单击绑定到日期的可标记文本字段时,会出现日期微调器。 Behind the datespinner I draw a sprite which needs to cover the whole screen so I can detect a click and make the datespinner go away. 在datepinner后面,我绘制了一个需要覆盖整个屏幕的精灵,这样我就可以检测到一个点击并让datepinner消失。

Now the problem- 现在的问题 -

Without overriding the get width or get height I haven't been able to fill the whole screen. 在没有覆盖获取宽度或获得高度的情况下,我无法填满整个屏幕。 However when I do this the datespinner breaks because its getting the height from the override. 但是,当我这样做时,datepinner会因为从覆盖中获得高度而中断。 Just wondering if anyone knew a way to override just one component and set all others to their default values. 只是想知道是否有人知道如何覆盖一个组件并将所有其他组件设置为默认值。 I know this might seem like a noob question and maybe I am missing something obvious, I am mostly new to as3. 我知道这可能看起来像一个菜鸟问题,也许我错过了一些明显的东西,我几乎是as3的新手。

Here is some code- 这是一些代码 -

override protected function createChildren():void {
        super.createChildren();
            if(!img) {
                img = new dateButtonBG();
                addChild(img);
            }

            if (!maskSprite) {
                maskSprite =  new Sprite();
                maskSprite.graphics.beginFill(0xFFFFCC, .5);
                maskSprite.graphics.drawRect(-((stage.height)/2),-((stage.width)/2), stage.width, stage.height);
                maskSprite.graphics.endFill(); 
            }

if(!dateButton) {
                dateButton = new StyleableTextField();
                todayDate = new Date();
                BindingUtils.bindProperty(dateButton, "text", date, "selectedDate");
                dateButton.addEventListener(MouseEvent.CLICK, onDateButtonClick);
                addChild(dateButton);
            }
invalidateDisplayList();
}

protected function removeSpinner( event:Event):void{
            maskSprite.removeEventListener(MouseEvent.CLICK, removeSpinner);
            removeChild(date);
            removeChild(maskSprite);
        }

protected function onDateButtonClick (event:Event):void {
            addChild(maskSprite);
            addChild(date);
            maskSprite.addEventListener(MouseEvent.CLICK, removeSpinner);
        }

override public function get width () : Number {
            return _width;
        }

override public function get height () : Number {
            return _height;
        }

The code is not complete but is just for getting my point across. 代码不完整,但仅仅是为了让我的观点得到解决。 Thanks for reading and all your help. 感谢阅读和所有帮助。

EDIT- I figured out how to do it.Adding some information in case some one has the same problem- Flex limits the size of your sprite( or any UI component) to the size of the container. 编辑 - 我想出了如何做到这一点。添加一些信息以防一些人有同样的问题 - Flex将精灵(或任何UI组件)的大小限制为容器的大小。 If you try to go over the size of the container it just returns the size of the container. 如果你试图超过容器的大小,它只返回容器的大小。 In my code- 在我的代码中 -

override public function get width () : Number {
            return _width;
        }

override public function get height () : Number {
            return _height;
        }

This is commonly touted as a fix to go over the size of the container. 这通常被吹捧为超过容器大小的修复程序。 This approach is flawed however because it overrides everything that asks for width and height. 然而,这种方法存在缺陷,因为它会覆盖要求宽度和高度的所有内容。 In this case it tries to make everything the size of _height and _width. 在这种情况下,它会尝试将所有内容设置为_height和_width的大小。 For any skin that has more than 1 component this is a huge problem, either you can try to set sizes for items indivigually, which for me didn't work or find an alternate approach. 对于任何具有多于1个组件的皮肤来说,这是一个很大的问题,要么您可以尝试单独设置项目的大小,这对我来说不起作用或找到替代方法。 Here is what works- 这是有效的 -

public override function validateDisplayList():void {
            super.validateDisplayList();
            yourComponent.width = someConstantW;
            yourComponent.height = someConstantH;
        }

This is not going to be enough however. 但这还不够。 You might need to move your component outside of the container for this try- 您可能需要将组件移出容器以进行此尝试 -

override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void {
                super.layoutContents(unscaledWidth, unscaledHeight);
                setElementPosition(yourComponent, x, y);
            }

Hopefully I saved someone a few hours of work :) 希望我救了几个小时的工作:)

It's not quite clear from the code you have posted, but perhaps you should be overriding the updateDisplayList() method instead of the width and height . 从您发布的代码中不太清楚,但也许您应该覆盖updateDisplayList()方法而不是widthheight

updateDisplayList() is the Flex life cycle method that components implement to size and position their child objects. updateDisplayList()是组件实现的子生命周期方法,用于调整子对象的大小和位置。

updateDisplayList() is called by the Flex framework, and it passes in two values: unscaledWidth and unscaledHeight . updateDisplayList()由Flex框架调用,它传递两个值: unscaledWidthunscaledHeight Your implmentation of updateDisplayList() should honor those values and size/position the child objects accordingly. 您对updateDisplayList()应该遵循这些值并相应地调整子对象的大小/位置。 You should use other life cycle methods (shown below) to do the actual sizing/positioning. 您应该使用其他生命周期方法(如下所示)来进行实际的尺寸调整/定位。

Here's an example implementation to get you started. 这是一个让您入门的示例实现。 It may be off a little, as I don't fully understand the code snippet you provided. 它可能有些偏差,因为我不完全理解您提供的代码段。

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
    super.updateDisplayList(unscaledWidth, unscaledHeight);
    // size/position the background (or whatever it is that should occupy the whole screen)
    background.setLayoutBoundsPosition(0,0); // unnecessary because 0,0 is default
    background.setLayoutBoundsSize(unscaledWidth, unscaledHeight);
    // size/position the text in the center
    var textWidth:Number = text.getPreferredBoundsWidth;
    var textheight:Number = text.getPreferredBoundsHeight
    text.setLayoutBoundsPosition( (unscaledWidth - textWidth)/2, (unscaledHeight - textHeight)/2 );
    text.setLayoutBoundsSize(textWidth, textHeight); // probably not necessary

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM