【问题标题】:child view of custom view getMeasureWidth return different value at different time自定义视图getMeasureWidth的子视图在不同时间返回不同的值
【发布时间】:2016-03-01 23:15:21
【问题描述】:

我有一个自定义视图,它有几个子视图,fabMenu 是其中一个 ImageView,我在 onMeasure() 方法中编写了以下代码

 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    measureChild(fabMenu, widthMeasureSpec, heightMeasureSpec);
    int width = fabMenu.getMeasuredWidth();
    int height = fabMenu.getMeasuredHeight();
    Log.d(TAG, "onMeasure: width " + fabMenu.getMeasuredWidth() + "  height " + fabMenu.getMeasuredHeight());
...
}

但每次日志显示不同的值

【问题讨论】:

  • 记录传递的widthMeasureSpecheightMeasureSpec参数并观察logcat,每次调用onMeasure时它们很可能不同
  • 是的,widthMeasureSpec 和 heightMeasureSpec 参数都变了,怎么会呢?我不知道。
  • 仔细阅读MeasureSpec 文档以了解onMeasure 参数的编码方式

标签: android view


【解决方案1】:

各位~

我终于“解决”了这个问题。

我之前将自定义视图放在 RelativeLayout 中,但现在我将其放入 FrameLayout(然后将 FrameLayout 放入父视图 - RelativeLayout )。

然后一切正常~

【讨论】:

    猜你喜欢
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 2016-09-14
    • 2019-06-04
    相关资源
    最近更新 更多