我动态创建了一堆文本字段,其数量取决于 xml 输入。文本字段需要启用按钮,所以我认为最简单的解决方案是将它们放入一个movieclip 和buttonMode 那个movieclip。
层次结构如下所示:main(扩展 MovieClip)-> klankoefening(一个 MovieClip)-> Textfieldparent(一个 MovieClip)-> Textfield
我有一个变量maxw,其中包含最大的文本字段宽度,我希望我所有的 Textfieldparents 都具有这个宽度。
我正在尝试这样做:
//generate textfields
keuzes = new XMLList(lijstOpties.categorie.(@cat == catname));
var nrs:int = keuzes.keuze.length(); //aantal keuzemogelijkheden
var maxw:int = 0; //grootste breedte tf, nodig voor bepalen breedte knoppen
for (var nr:int = 0; nr < nrs; nr++) {
var tf:TextField = createTextfield(werkfmt);
tf.text = keuzes.keuze[nr].toString();
tf.autoSize = "center";
tf.background = true;
tf.backgroundColor = 0xffcc33;
tf.border = true;
tf.borderColor = 0x000000;
tf.name = "keuzetf";
tf.selectable = false;
tf.x = 0;
tf.y = 0;
if (tf.width > maxw) {maxw = tf.width;}
var tfcontainer:MovieClip = new MovieClip();
tfcontainer.buttonMode = true;
tfcontainer.name = keuzes.keuze[nr].toString();
tfcontainer.addChild(tf);
klankoefening.addChildAt(tfcontainer,nr);
}
for (var an:int = 0; an < 3; an++) {
var mcs:MovieClip = klankoefening.getChildAt(an) as MovieClip;
mcs.width = maxw;
trace ("mcs " + mcs.width);
var mtf:TextField = mcs.getChildAt(0) as TextField;
trace ("mtf " + mtf.text);
}
跟踪结果是
mcs 32.05
mcs 33
mcs 21.25
我觉得很奇怪,因为我在第二个循环中声明所有 Textfieldparents 的宽度都应该设置为maxw。怎么了?