【问题标题】:ActionScript 3 Distributing TextFieldsActionScript 3 分发文本字段
【发布时间】:2009-12-17 14:24:43
【问题描述】:

动作脚本 3

我今天一直在寻找解决这个问题的最佳方法,但最终我把我认为很简单的任务复杂化了。

我有一些 TextFields 彼此相邻排列,所有固定宽度,即 ( [ width ])

      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     ]

任何关于更好方法的想法,我确信我可以使用一些不错的简单功能来做到这一点。

我现在可能也将问题过于复杂了,但我试一试,有人有什么想法吗?

【问题讨论】:

    标签: flash actionscript-3 textfield alignment distribute


    【解决方案1】:

    所以,让我直截了当地说,你有 N 个不同宽度的文本字段,你想删除其中的 1 个或多个,重新排列剩余的 M 个文本字段,按比例重新分配它们的宽度(即,如果文本字段 T1 是文本字段 T2 宽度的两倍,重新排列后仍然是)?

    这实际上不是一个非常简单的问题,但我在下面尝试了一下。您将文本字段的数组(从左到右)、要保留的数字和可选的 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;
      }  
    }
    

    注意:此代码实际上并未从视图中删除“未保留”的文本字段;你必须添加它或在其他地方做。

    【讨论】:

      【解决方案2】:

      我是不是理解有误,还是这么简单:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多