【问题标题】:Set maxHeight for a ViewGroup为 ViewGroup 设置 maxHeight
【发布时间】:2014-10-08 08:31:45
【问题描述】:

ViewGroup 有一个名为minHeight 的属性,允许您设置ViewGroup 的最小高度。

http://developer.android.com/reference/android/view/View.html#attr_android:minHeight

没有名为 maxHeight 的属性。那么究竟如何设置ViewGroup 的最大高度呢?看来您必须在 ViewGroup 的重写方法中而不是在 XML 中做一些事情。我试图在onMeasure() 中做这样的事情,但没有运气。有人有什么想法吗?

更新

具体来说,我想设置 height="wrap_content" 同时还设置一个 maxHeight FlowLayout

FlowLayout 内的视图没有显示出来。

【问题讨论】:

    标签: android height max viewgroup layoutparams


    【解决方案1】:

    您必须覆盖onMeasure() 并调用setMeasuredDimension。比如

        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mMaxHeight);
    

    其中 mMaxHeight 可以是一个固定值,或者您可以循环查看 ViewGroup 的子项,测量它们并计算该值

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 1970-01-01
      • 2012-03-28
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2022-11-12
      相关资源
      最近更新 更多