【问题标题】:How can I convert width in percentage to a number value?如何将百分比宽度转换为数值?
【发布时间】:2012-10-17 22:54:59
【问题描述】:

我正在创建一个宽度 = 20% 的视图。我怎样才能得到一个整数值。

var v = Ti.UI.createView({
    width: '20%',
    left: '10%',
    height: '40%',
    top: '10%'
});

我需要将 v.width 转换为数字。我该怎么做?

【问题讨论】:

  • 在运行时获取屏幕的宽高,然后计算百分比。
  • 所以你告诉我不要按原样使用百分比,而是手动计算它,因为 20% 是 v.width = mainwin.width * 0.20,其中 mainwin.width = Ti.Platform.DisplayCaps .PlatformWidth?
  • 是的,我是这个意思。你有下面的答案。

标签: android view width titanium-mobile


【解决方案1】:

有一个功能可以做到这一点:

function turnPercentToDp(percent){

    var dp = 0;

    if(percent){

      if(_.isString(percent) && percent.indexOf('%')>0) {
        percent = parseFloat(percent.slice(0, percent.indexOf('%')))/100;
      }else{
        if(percent > 1){
          percent = parseFloat(percent)/100;
        }
      }

      dp = Math.ceil((OS_IOS ? Ti.Platform.displayCaps.platformWidth : (Ti.Platform.displayCaps.platformWidth/Ti.Platform.displayCaps.logicalDensityFactor))*percent);

    }

    return dp;

}

【讨论】:

    【解决方案2】:

    请试试这个:

    var screenWidth = Titanium.Platform.displayCaps.platformWidth;
    
    var v = Ti.UI.createView({
       width: screenWidth/5 ,
       left: '10%',
       height: '40%',
       top: '10%'
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      相关资源
      最近更新 更多