简体   繁体   English

Flex - 自定义组件 - 百分比宽度/高度

[英]Flex - Custom Component - Percentage Width/Height

I am trying to create a Custom Flex Component using the Flex Component framework: 我正在尝试使用Flex组件框架创建自定义Flex组件:

http://www.adobe.com/livedocs/flex/3/html/help.html?content=ascomponents_advanced_3.html . http://www.adobe.com/livedocs/flex/3/html/help.html?content=ascomponents_advanced_3.html

All good components allow you to define their dimensions using percentage values using: 所有好的组件都允许您使用百分比值来定义它们的尺寸:

MXML: MXML:

TextInput width="100%" TextInput width =“100%”

or 要么

Actionscript at runtime: 运行时的Actionscript:

textinp.percentWidth = 100; textinp.percentWidth = 100;

My question is how do you implement percentage width/height in the measure() method of your custom component? 我的问题是如何在自定义组件的measure()方法中实现百分比宽度/高度? More specifically, these percentages are converted to pixels values at some stage, how is this done? 更具体地说,这些百分比在某个阶段转换为像素值,这是如何完成的?

The way Flex layouting works is by letting each component lay out its children to a size specified by the parent. Flex布局的工作方式是让每个组件将其子项布局为父项指定的大小。 "Children" does include the usual children - getChildren(), numChildren - and also those components which makes up borders, handles, etc., which are called "chrome" and are part of getRawChildren(). “Children”确实包括通常的孩子 - getChildren(),numChildren - 以及组成边框,句柄等的组件,这些组件被称为“chrome”并且是getRawChildren()的一部分。

The Flex framework now does two loops over all components (which are actually part of the display tree). Flex框架现在对所有组件(实际上是显示树的一部分)执行两个循环。 The first is bottom up (deepest nested elements first) and call the measure() method. 第一个是自下而上(首先是最深的嵌套元素)并调用measure()方法。 It should calculate how much space (width/height) the component would take if it can has as much space as it wants, no limits (think: scrollbars) and put it into the measuredWidth and measuredHeight properties. 如果组件可以拥有所需的空间,则应该计算组件将占用多少空间(宽度/高度),没有限制(想想:滚动条)并将其放入measuredWidth和measuredHeight属性中。 Secondly, it calculates how much space it wants to have as an absolute minimum, put into measuredMinHeight / measuredMinWidth. 其次,它计算了它想要的绝对最小空间,放入measuredMinHeight / measuredMinWidth。 To calculate this, border thickness, chrome and regular children are asked about their sizes using getExplicitOrMeasuredHeight()/Width(), and added together. 为了计算这一点,使用getExplicitOrMeasuredHeight()/ Width()询问边框粗细,铬和常规子项的大小,并将它们相加。 That's why it's depth-first. 这就是为什么它是深度优先的原因。

The second loop is to do the actual layouting. 第二个循环是进行实际布局。 This starts at the top of the tree, and updateDisplayList is called, with x/y parameters telling the component how much size it actually does have. 这从树的顶部开始,并调用updateDisplayList,x / y参数告诉组件它实际具有多少大小。 Based on this information the component will tell its direct children where they should be (relative to their parents) and how big they should be - child.move(x,y) and child.setActualSize(w,h) 根据这些信息,该组件将告诉其直接的孩子他们应该在哪里(相对于他们的父母)以及他们应该有多大 - child.move(x,y)和child.setActualSize(w,h)

So it's actually the parent container who takes relative sizes into account. 因此,它实际上是将相对大小考虑在内的父容器。 Your component states it's ideal (minimum size so that everything can be displayed) and minimum sizes in measure() and has to deal with whatever it gets in its updateDisplayList(). 您的组件声明它是理想的(最小大小以便可以显示所有内容)和measure()中的最小大小,并且必须处理它在updateDisplayList()中获得的任何内容。 The parent component takes the available space it has, makes sure that each component gets it's minimum size and will then distribute the rest to all components. 父组件获取它具有的可用空间,确保每个组件获得它的最小大小,然后将其余组件分配给所有组件。 In this phase, it will look at the percentWidth/Height properties of its children. 在此阶段,它将查看其子项的percentWidth / Height属性。

So if you want to put your component in a standard Flex container like HBox, you don't need to do anything special for percentage sizes to work. 因此,如果您想将组件放在像HBox这样的标准Flex容器中,则无需对百分比大小进行任何特殊操作。

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

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