小菜AS3之路

2012.2.17
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

DataGrid

Posted on 2012-08-20 17:39  枫雨  阅读(226)  评论(0编辑  收藏  举报

flash cs3中使用DataGrid组件时, 要改变单元格里的字体大小颜色真是麻烦!
以下是方法:

var list:DataGrid = new DataGrid();
//其他的数据,宽高什么的等属性自己设去吧;

////////TextFormat////////
var tf1:TextFormat = new TextFormat();
tf1.size = 12;
tf1.color = 0x000000;
//其他的属性自己调;

var tf2:TextFormat = new TextFormat();
tf2.size = 13;
tf2.color = 0xff0000;
tf2.bold = true;
//其他的属性自己调;
////////TextFormat////////

//下面是应用:
list.setRendererStyle("textFormat", tf1);//这是设置表头上的单元格文字格式;
list.setStyle("headerTextFormat", tf2);//这是设置表内单元格上的文字样式;

以前是表格内所有单元格沿用一种样式, 但有时候希望某一列的样式要不一样. 比方说我要第一列的内容居中,其他的还是上面那种样式呢?
那就看下面:

新建一个类:

package {
    import fl.controls.listClasses.CellRenderer;
    import fl.controls.listClasses.ICellRenderer;
    import flash.text.TextFormat;
    
    public class CellStyle1 extends CellRenderer implements ICellRenderer {

        public function CellStyle1():void {
            super();
        }

        override protected  function drawBackground():void {
            var format:TextFormat=new TextFormat  ;

            format.color=0x000000;
            format.size=12;
            format.align = "center";
            //其他属性自己设;
            setStyle("textFormat",format);
            super.drawBackground();
        }
    }
}

应用:

//将第一列所有单元格的样式设置为CellStyle1里的format所具有的样式;单元格表头不在范围内;
list.getColumnAt(0).cellRenderer = CellStyle1;

//当然也可以一次性设置同一种样式;
list.setStyle("cellRenderer",CellStyle1);
(如果CellStyle1里的format的样式效果和tf1相同的话,那么上面效果等同于:
list.setRendererStyle("textFormat", tf1);//这是设置表头上的单元格文字格式;

如果每列都要不同的效果可能就要多个CellStye1类吧!