高手进-flex4自定义控件的问题

kojie_chen 2011-05-16 02:59:31
	<koje:CustomPanel id="k1" x="100" y="100" x.fullCu="0" y.fullCu="0" title="1"  width.default="141" width.fullCu="1000" height.fullCu="800" height.default="146" restore="restoreState()" max="maxState('fullCu')">
<s:Button label="按钮" x="10" y="10"/> </koje:CustomPanel>

为什么我自定义panel控件以后。在里面放一个button,虽然设置了x.y,但是button 还是在0,0位置。坐标设置好像不起作用,求解。基于flex4
...全文
179 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kojie_chen 2011-05-16
  • 打赏
  • 举报
回复
找到原因了。看了一下panel的api。看到panel的layout,默认为vertical,我应该在构造函数里面,设置panel的layout为absolute ,这样就能设置x,y了。谢谢hero兄
kojie_chen 2011-05-16
  • 打赏
  • 举报
回复
package com.koje
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;

import mx.containers.Panel;
import mx.controls.Alert;
import mx.controls.Button;

[Event(name="restore")]
[Event(name="max")]
public class CustomPanel extends Panel
{
private var close_btn:Button;
private var max_btn:Button;
private var restore_btn:Button;
private var state:uint = 0;
[Embed("/images/panelClose.png")]
private var CloseIcon:Class;
[Embed("/images/panelHide.png")]
private var HideIcon:Class;
[Embed("/images/panelShow.png")]
private var ShowIcon:Class;
public function CustomPanel()
{
super();
this.title = "Custom Panel";

}
private function setState(s:uint):void
{
this.state = s;
if(state == 0)
this.dispatchEvent(new Event("restore"));
else
this.dispatchEvent(new Event("max"));
}
public function clickHandle(evt:MouseEvent):void
{
Alert.show("hide");
// this.visible = false;
}
override protected function createChildren():void
{
super.createChildren();
if(!this.close_btn)
this.close_btn = new Button;
this.close_btn.setStyle("overIcon",CloseIcon);
this.close_btn.setStyle("upIcon",CloseIcon);
this.close_btn.setStyle("downIcon",CloseIcon);

this.close_btn.x = 10;

this.max_btn = new Button;
this.max_btn.setStyle("overIcon",HideIcon);
this.max_btn.setStyle("upIcon",HideIcon);
this.max_btn.setStyle("downIcon",HideIcon);
this.max_btn.x = 50;


this.restore_btn = new Button;
this.restore_btn.setStyle("overIcon",ShowIcon);
this.restore_btn.setStyle("upIcon",ShowIcon);
this.restore_btn.setStyle("downIcon",ShowIcon);

this.restore_btn.x = 100;

this.rawChildren.addChild(max_btn);
this.rawChildren.addChild(restore_btn);
this.rawChildren.addChild(close_btn);
this.max_btn.addEventListener(MouseEvent.CLICK,doMax);
this.restore_btn.addEventListener(MouseEvent.CLICK,doRestore);
this.close_btn.addEventListener(MouseEvent.CLICK,clickHandle);
}
public function doMax(evt:MouseEvent):void
{
setState(1);
Alert.show("Max");
}
public function doRestore(evet:MouseEvent):void
{
setState(0);
Alert.show("Restore");
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth,unscaledHeight);
if(this.unscaledWidth >0)
this.visible = true;
else
this.visible = false;
var closeAsset:DisplayObject = this.close_btn.getChildByName("upIcon");
var maxAsset:DisplayObject = this.max_btn.getChildByName("upIcon");
var resAsset:DisplayObject = this.restore_btn.getChildByName("upIcon");
var margin:int = 4;

this.close_btn.setActualSize(closeAsset.width + margin,closeAsset.height + margin);
this.max_btn.setActualSize(maxAsset.width + margin,maxAsset.height + margin);
this.restore_btn.setActualSize(resAsset.width + margin,resAsset.height+ margin);

var pixelsFromTop:uint = 1;
var pixelsFromRight:uint = 10;
var btnwidth:uint = close_btn.width;
var x:Number = unscaledWidth - btnwidth - pixelsFromRight;
var y:Number = pixelsFromTop;
close_btn.move(x,y);
max_btn.move(x-35,y);
restore_btn.move(x-70,y);
}

}
}

上面是源码。并没有对layout进行设置过。
herowach 2011-05-16
  • 打赏
  • 举报
回复
容器要是BasicLayout才能使用绝对定位的,你看看是不是自定义panel里面是否设了layout
kojie_chen 2011-05-16
  • 打赏
  • 举报
回复
	<koje:CustomPanel id="k1" x="100" y="100" x.fullCu="0" y.fullCu="0" title="1"  width.default="141" width.fullCu="1000" height.fullCu="800" height.default="146" restore="restoreState()" max="maxState('fullCu')">
<s:Button label="按钮" x="10" y="10"/>
</koje:CustomPanel>
Flex-超级学习资料集锦,其中包含多本 Flex pdf 书籍,和 API Flex4.0API 中文版,都是 Flex 开发 的必读书籍,和 不可缺少的 API 神器,还有,一个超级项目,水果忍者,几乎包含了,flex 的 所有技术,强大的 UI界面实现,以及 超强的 逻辑控制,都是 学习 技术的 终极训练,通过这个项目,基本上,都是 Flex 界的 高手,;同时,在书籍中,Flex 书籍,本不多,更别说, 中文版的了,Flex 资料不同于 java ,多得一塌糊涂,书籍中,包含了,非常畅销的 《Flex 从入门到实践》 和 《Flex 从入门到精通》 ,这两本书 可以说是 Flex 中文书籍的 权威之作,和,精品之作,两本书都: 按照Flex技术点,由浅入深、循序渐地对Flex的开发工具、基本语法、基本的容器和控件的语法及使用、高级编程、自定义的各类组件、页面的风格和主题、Flex与HTML的通信、共享对象、远程数据的控制、Flex图表及Cairngorm框架等行了全面、系统的讲解。《Flex从入门到精通》最后一章给出了一个完整的实例,读者从中可亲身体验到实际项目开发的全过程,更快更好地全面掌握Flex技术。Flex是RIA(Rich Internet Applications,富媒体)时代最领先和成熟的产品。同时,都又有着 各自的 特点,是 学习的必读教材,相信有了这个 《Flex-超级学习资料集锦》 精通都是小事,是 你 开发之路的 良师益友,和 学习 的强大 工具。

4,327

社区成员

发帖
与我相关
我的任务
社区描述
多媒体/设计 Flex
社区管理员
  • Flex
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧