2009-12-17 35 views
0

ActionScript 3的ActionScript 3的分布的TextField

我已经冲倒我的头今天最上要对此最好的办法,但我最终在复杂的什么,我会认为是一个简单的任务。

我有一些的TextField一字排开彼此相邻的所有固定宽度,即([宽])

 tf1   tf2  tf3  tf4  tf5 
[  80  ][ 50 ][ 50 ][ 50 ][ 70 ] 

我想要做的是能够选择我要显示其细节,然后将删除的TextField的宽度重新分配给其余部分(注意只能从右侧删除),所以如果我要显示“tf3”,则需要将120(70 + 50)均匀分配到其他框:

[  80 + 50 + 50 = 180     ] 
      tf1 
[80/180*120 + 80 = 133.3] 
         tf2 
       [50/180*120 + 50 = 83.3] 
            tf3 
         [50/180*120 + 50 = 83.3] 

然后排队起来再次(X +宽度等):

[  133.3  ][  83.3  ][  83.3  ] 

任何想法的更好的方法,我肯定有一些不错的简单功能,我可以用来做到这一点。

我可能已经过于复杂的问题现在也,但生病给它一个尝试,任何人有什么想法?

回答

1

因此,让你有不同宽度N个文本字段我得到这个直,你想删除1或者更多,重新排列剩余的M个文本字段,按比例重新分配它们的宽度(即,如果文本字段是文本字段T的宽度的两倍,那么在它们重新排列后它仍然会是)。

这实际上不是一个非常简单的问题,但我在下面进行了刺探。您将文本字段数组(从左到右),要保留的数字以及可选的X偏移量传递给它。

注意:我还没有在几个月内完成AS3所以考虑下面是伪代码 - 你将不得不编辑它才能工作。

function arrangeTextFields(textFields:Array, keep:int, xOffset:Number = 0) { 
    // we can't keep more fields than are provided 
    keep = Math.min(keep, textFields.length); 

    var totalWidth = 0, // the total width of all textfields 
     keptOrigWidth = 0, // the total combined widths of the ones you're keeping 
     origNum:int = textFields.length; 

    // calculate the above values by addition in a loop 
    // (because AS3 lacks an inject/reduce method) 
    for(var i:int, i++, i < origNum) { 
    totalWidth += textFields[i].width; 

    if(i < take) { 
     keptOrigWidth += textFields[i].width; 
    } 
    } 

    var ratio:Number = totalWidth/takenOrigWidth, // the ratio we'll resize each field by to keep them proportional 
     currentX:int = 0; 

    textFields = textFields.slice(0, keep - 1); // throw away the fields we're not keeping 

    for(int i = 0; i < take; i++) { 
    textFields[i].width = textFields[i].width * ratio; // resize it 
    textFields[i].x  = currentX + xOffset;   // move it 

    currentX += textFields[i].width; 
    } 
} 

注:此代码实际上并没有从视图中删除了“unkept”文本字段;你必须添加它或在别处做。

1

上午我错理解的东西,或者是不是很简单:

boxWidth = (availableWidth - (padding + margins))/numberOfFieldsShown