android - Titanium:当布局=垂直时隐藏 View
<p><p>Titanium UI 中的一个常见问题是在垂直布局中隐藏 View 。</p>
<p>假设我们有这个:</p>
<pre><code><Alloy>
<View id="wrapper" layout="vertical">
<Label id="sometimes_visible" top="20" height="50">I can be visible o not</Label>
<Label id="always_visible" top="20" height="50">I'm always visible</Label>
</View>
</Alloy>
</code></pre>
<p>而你,出于某种原因,需要隐藏 <code>sometimes_visible</code> 标签:</p>
<pre><code>$.sometimes_visible.visible = false;
</code></pre>
<p>也许你期望的结果是:</p>
<pre><code>_______________________________
| ________________________ |
| |I'm always visible| |
| ------------------------ |
|______________________________|
</code></pre>
<p>相反,你得到:</p>
<pre><code>_______________________________
| |
| |
| |
| ________________________ |
| |I'm always visible| |
| ------------------------ |
|______________________________|
</code></pre>
<p>(<code>always_visible</code> 标签上方的多余空间)</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这是因为在 Titanium 中,<code>visible=false</code> 只是将 View 设置为不可见,但它仍然占据其空间。因此,在垂直布局中,其他 View 不会重新排列以填补空白(这是正确的,即使不需要)。</p>
<p>解决这个问题的 fragment 如下:</p>
<pre><code>/**
* hides a view nested into a layout=vertical container
* acts on top, bottom and height to simulate html-style display:none
* @param{Ti.View} view the view to be hidden
*/
function hideVertical(view) {
//store previous values
view.__originalValues = {
top: view.top,
bottom: view.bottom,
height: view.height
};
//set new values to simulate invisibility
view.top = 0;
view.bottom = 0;
view.height = 0;
view.visible = false;
}
/**
* shows a view nested into a layout=vertical container
* restore from hideVertical()
* @param{Ti.View} view the view to be shown
*/
function showVertical(view) {
//restore previous values
view = _.extend(view, view.__originalValues || {});
view.visible = true;
}
</code></pre>
<p>可以在 Controller 的代码中简单地实现:</p>
<pre><code>hideVertical($.sometimes_visible);
</code></pre>
<p> <a href="https://gist.github.com/balanza/c396ac1f5013fd586ce4" rel="noreferrer noopener nofollow">gist</a> </p></p>
<p style="font-size: 20px;">关于android - Titanium:当布局=垂直时隐藏 View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28024770/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28024770/
</a>
</p>
页:
[1]