【问题标题】:How to hide/show view in Titanium?如何在 Titanium 中隐藏/显示视图?
【发布时间】:2013-05-13 09:58:47
【问题描述】:

我是 Titanium 的新手。

我已经查看了 3 个视图,并希望在 按钮点击 iPhone 应用时隐藏显示该视图。

知道如何实现吗?

【问题讨论】:

  • 你能说得更具体点吗?你真正想要做什么,你有没有尝试过任何代码等等。

标签: iphone titanium titanium-mobile


【解决方案1】:

您可以非常轻松地隐藏/显示视图,这是一个自包含的示例:

var win = Ti.UI.createWindow();
var view = Ti.UI.createView({
   width : 100,
   height : 100,
   backgroundColor : 'red' 
});

var redView = Ti.UI.createView({
    title : 'Hide / Show Red View',
    bottom : 0,
    width : 200,
    height : 35
});
var visible = true;
button.addEventListener('click', function(e) {
   if(visible)  {
       redView.hide();
   } else {
       redView.show();
   }
   visible = !visible;
});

win.add(redView);
win.add(button);
win.open();

【讨论】:

    【解决方案2】:

    虽然另一个答案当然有用,但另外两种(略有)不同的方法可以做同样的事情,以防万一当你使用 show() 和 hide() 时 Titanium 会翻转。

    //Method 1, using part of Josiah Hester's code snippet
    var visible = true;
    button.addEventListener('click', function(e) {
       if(visible)  {
           redView.setVisible(false); //This is the exact same thing as hide() method
       } else {
           redView.setVisible(true); //This is the exact same thing as show() method
       }
       visible = !visible;
    });
    

    您可以将不透明度设置为 0,即使视图的可见属性设置为 true,它也将仍然不可见,因为完全不存在不透明度级别。如果您希望某些内容可见但不可点击(通过将视图放在不透明度为零的视图后面),这很有用。

    //Method 2, same code section
    var opacity = 0;
    button.addEventListener('click', function(e) {
       if(opacity)  {
           redView.setOpacity(0); //This is the NOT the same thing as hide() method
       } else {
           redView.setOpacity(1); //This is the NOT thesame thing as show() method
       }
       opacity = Math.abs(opacity - 1);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2012-04-28
      相关资源
      最近更新 更多