【问题标题】:How to get ViewSwitcher to wrap only the currently displayed view height?如何让 ViewSwitcher 仅包装当前显示的视图高度?
【发布时间】:2011-07-24 21:42:27
【问题描述】:

我正在使用ViewSwitcher,它让我可以轻松地在两个不同的内置动画列表标题之间来回切换。

但是,问题在于两个视图的高度差异很大,并且带有layout_height="wrap_content" 的 ViewSwitcher 似乎测量了两个孩子并将两个高度中的较大者作为自己的高度。这会导致在显示较小的视图时在其正下方出现大量不需要的空白空间。

我理解为什么这是所需的 ViewSwitcher 行为,但它不是所需的 My App 行为。

示例:
假设视图 A 的高度为 50dp,视图 B 的高度为 100dp,并且两者都作为子项添加到 ViewSwitcher。即使显示视图 A,ViewSwitcher 也始终是 100dp 高。

我想要的是能够在 A 和 B 之间切换,并使 ViewSwitcher 缩小或增大以适应当前显示的任何视图。我该怎么做?

【问题讨论】:

    标签: android android-layout android-listview android-animation


    【解决方案1】:

    ViewSwitcher 上的measureAllChildren 属性设置为false

    <ViewSwitcher
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:measureAllChildren="false">
    

    欲了解更多信息,请参阅documentation

    【讨论】:

    • 正是我想要的。谢谢!我认为在框架布局中默认设置为 false,但我猜 ViewSwitcher 会覆盖它。
    • 是的,默认为假。但是,您可以使用主题覆盖它。显然 Android 的默认主题就是这样做的,所以现在,默认情况下确实如此。 :) 谷歌可能应该更改文档或代码。
    猜你喜欢
    • 2011-05-23
    • 2016-04-01
    • 2020-01-01
    • 1970-01-01
    • 2011-02-15
    • 2013-01-09
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多