Flash AS3

编程入门 行业动态 更新时间:2024-10-22 19:22:24
Flash AS3 - 彩色用户名列表框不同颜色(Flash AS3 - Colorized username list box different colors)

我找到了几个关于CellRenderer的教程,它们将改变整个列表或选定的项目,但我对AS3还不是很好,需要一些帮助。 我正在从xml文件加载用户列表。 一切都很好,很好,但我想要做的实际上是由小组着色。

再次,它加载到列表就好了,我想要做的是在循环中添加它们从xml文件,如果它的管理员使其为红色字体,绿色字体为mod,黑色为成员。

有什么帮助吗?

I have found several tutorials regarding CellRenderer that will change an entire list or selected item, but Im not all that good with AS3 yet and need some help. I am loading a userlist from an xml file. Everything loads fine and well, but what Im looking to do is actually colorize it by the group.

Again, it loads to the list just fine, what I am looking to do is in the loop that adds them from the xml file, if its admin make it red font, green font for mod, black for member.

Any help please?

最满意答案

有几点需要注意:

如果您计划使用字体格式,还需要将渲染器样式 embedFonts 设置为true 由于您要为单个单元格设置样式,因此首先需要等待填充列表(完成组件生命周期的内部数据设置部分)。 这可以通过调用drawNow()或invalidate()来强制 最后,在渲染处理程序中,通过itemToCellRender()方法访问单个单元格渲染器以设置样式。

这是一个基本的例子(假设a,b,c为admin,mod,member):

//in a setup function { //some dummy data var data:XML = <users> <user name="user 1" group="a" /> <user name="user 2" group="a" /> <user name="user 3" group="a" /> <user name="user 4" group="b" /> <user name="user 5" group="b" /> <user name="user 6" group="b" /> <user name="user 7" group="c" /> <user name="user 8" group="c" /> <user name="user 9" group="c" /> </users> //create a list var list:List = addChild(new List()) as List; list.setSize(600,400); list.move(0,400); //setup a font AND set embefFonts to true list.setRendererStyle("textFormat",new TextFormat("Siemens Sans SC Black",11,0xFF9900)); list.setRendererStyle("embedFonts",true); //populate list for(var i:int = 0 ; i < data.user.length(); i++) list.addItem({label:String(data.user[i].@name),group:String(data.user[i].@group)}); list.invalidate();//tell list to refresh itself list.addEventListener(Event.RENDER,listUpdated);//listen for the refresh } //after the list refreshed private function listUpdated(event:Event):void{ //setup text formats var formats:Dictionary = new Dictionary(); formats["a"] = new TextFormat("Siemens Sans SC Black",11,0x990000); formats["b"] = new TextFormat("Siemens Sans SC Black",11,0x009900); formats["c"] = new TextFormat("Siemens Sans SC Black",11,0x000099); var list:List = List(event.currentTarget); for(var i:int = 0 ; i < list.dataProvider.length; i++) { var item:Object = list.getItemAt(i); var cr:CellRenderer = CellRenderer(list.itemToCellRenderer(item));//get the individual cell renderers cr.setStyle("textFormat",formats[item.group]); //set styles per cell } list.removeEventListener(Event.RENDER,listUpdated);//clear listener }

There are couple of caveats to be aware of:

If you're planning to use a font format, also need to set the renderer style embedFonts to true Since you want to set styles for individual cells, first you need to wait for the list to be populate (finish it's internal data setup part of the component's lifecycle). This can be forced by calling drawNow() or invalidate() Finally, in the render handler access individual cell renderers via the itemToCellRender() method to set the styles.

Here's a basic example(and imagine a,b,c as admin,mod,member):

//in a setup function { //some dummy data var data:XML = <users> <user name="user 1" group="a" /> <user name="user 2" group="a" /> <user name="user 3" group="a" /> <user name="user 4" group="b" /> <user name="user 5" group="b" /> <user name="user 6" group="b" /> <user name="user 7" group="c" /> <user name="user 8" group="c" /> <user name="user 9" group="c" /> </users> //create a list var list:List = addChild(new List()) as List; list.setSize(600,400); list.move(0,400); //setup a font AND set embefFonts to true list.setRendererStyle("textFormat",new TextFormat("Siemens Sans SC Black",11,0xFF9900)); list.setRendererStyle("embedFonts",true); //populate list for(var i:int = 0 ; i < data.user.length(); i++) list.addItem({label:String(data.user[i].@name),group:String(data.user[i].@group)}); list.invalidate();//tell list to refresh itself list.addEventListener(Event.RENDER,listUpdated);//listen for the refresh } //after the list refreshed private function listUpdated(event:Event):void{ //setup text formats var formats:Dictionary = new Dictionary(); formats["a"] = new TextFormat("Siemens Sans SC Black",11,0x990000); formats["b"] = new TextFormat("Siemens Sans SC Black",11,0x009900); formats["c"] = new TextFormat("Siemens Sans SC Black",11,0x000099); var list:List = List(event.currentTarget); for(var i:int = 0 ; i < list.dataProvider.length; i++) { var item:Object = list.getItemAt(i); var cr:CellRenderer = CellRenderer(list.itemToCellRenderer(item));//get the individual cell renderers cr.setStyle("textFormat",formats[item.group]); //set styles per cell } list.removeEventListener(Event.RENDER,listUpdated);//clear listener }

更多推荐

本文发布于:2023-07-08 19:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Flash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!