DataGrid自动调整行高

这是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 copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, 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 IN
	THE 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 square
									override 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.  The
							HBox will use the Text's measuredHeight to determine its own measuredHeight, and
							the DataGrid will use the HBox's measuredHeight in laying out the row.  The list classes
							set the explicitWidth of the HBox to the column or list width before measuring
							the HBox and asking for its measuredHeight.  We have to
							tie the width to the explicitWidth given to the renderer (the HBox) and factor in
							the 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>
 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值