DataGrid自动调整行高

来源:互联网 发布:淘宝美工主页设计 编辑:程序博客网 时间:2024/04/29 16:40

这是Alex Harui写的一个关于自定义的基于流式的renderer使用演示。

                      首先,它在左上角画了一个正方形,然后设置显示文本在右侧。每一行的文本值都都不一样,有多的,也有少的。

                      这个例子使用了数据绑定来设置 mx:Text文本的宽度,所以它会获取到正确的measuredHeight值。这样的话,通过调用measureHeightOfItems可以让DataGrid计算到合适的高度。

                      好了,看例子吧。

 

 

<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"xmlns="*"height="600" updateComplete="sizeDGExactly()" viewSourceURL="srcview/index.html"><!--Copyright (c) 2010 Alex Harui, Adobe Systems Inc.Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.--><mx:Script><![CDATA[private var dp:Array = [{ name: "Blue", color: 0x0000FF, comment: "This is the color of blueberries and blue cheese." },{ name: "Green", color: 0x00FF00, comment: "This is the color of beans, cucumbers, peas, spinach, lettuce, cabbage and limes." },{ name: "Yellow", color: 0xFFFF00, comment: "This is the color of lemons and squash." },{ name: "Orange", color: 0xFF8000, comment: "This is the color of carrots." },{ name: "Red", color: 0xFF0000, comment: "This is the color of tomatoes, cherries, some apples, some potatoes and beets." }]; private function sizeDGExactly():void{if (dg){dg.height = dg.measureHeightOfItems(-1, // have start with index = -1 so header gets measured dp.length + 1) // have to ask for one extra row so header gets measured+ dg.viewMetrics.top // add in top border+ dg.viewMetrics.bottom; // and bottom border}}]]></mx:Script><mx:DataGrid id="dg" initialize="dg.dataProvider = dp" width="50%" variableRowHeight="true"><mx:columns><mx:DataGridColumn dataField="name" /><mx:DataGridColumn dataField="name"><mx:itemRenderer><!-- custom renderer that draws a colored square next to the word-wrapping text --><mx:Component><mx:HBox horizontalGap="0"><mx:Script><![CDATA[// this method draws the colored squareoverride protected function updateDisplayList(w:Number, h:Number):void{super.updateDisplayList(w, h);square.graphics.clear();square.graphics.beginFill(data.color);square.graphics.drawRect(0, 0, 20, 20);square.graphics.endFill();}]]></mx:Script><!-- this UIComponent holds the colored square --><mx:UIComponent id="square" width="20" height="20" /><!-- This is the key point.  The measured height of the Text depends on its width.  TheHBox will use the Text's measuredHeight to determine its own measuredHeight, andthe DataGrid will use the HBox's measuredHeight in laying out the row.  The list classesset the explicitWidth of the HBox to the column or list width before measuringthe HBox and asking for its measuredHeight.  We have totie the width to the explicitWidth given to the renderer (the HBox) and factor inthe width of the square as well (hence the '- 20'). --><mx:Text id="comment" width="{explicitWidth - 20}" height="100%" text="{data.comment}" /></mx:HBox></mx:Component></mx:itemRenderer></mx:DataGridColumn>            </mx:columns></mx:DataGrid></mx:Application> 


 

原创粉丝点击