小编use*_*994的帖子

强制重新布局视图组,包括其所有子项

我正在编写一个FrameLayout可以放大的自定义布局(扩展).它的所有子节点也是自定义视图,它实际上通过getter方法从父节点获得比例因子,并通过设置缩放尺寸来相应缩放

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    float scaleFactor = ((CustomLayout) getParent()).getCurrentScale();
    setMeasuredDimension((int) (getMeasuredWidth() * scaleFactor), (int) (getMeasuredHeight() * scaleFactor));
}
Run Code Online (Sandbox Code Playgroud)

我正在使用a ScaleGestureDetector来检测"缩放缩放"手势并更改布局的scaleFactor.然后我通过调用强制在自定义布局上的布局requestLayout.不幸的是,这似乎对其孩子没有任何影响.孩子们的onMeasure&onLayout即使父经历其措施和布局周期永远不会被调用.但是,如果我直接打电话requestLayout给其中一个孩子,那么这个孩子就会根据父母设定的比例因子进行缩放!

似乎除非requestLayout在视图上专门调用,否则它实际上不会再次测量自身,而是使用某种缓存.从视图的源代码中可以看出这一点

if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) {
        // Only trigger request-during-layout logic if this is the view requesting it,
        // not the views in its parent hierarchy
        ViewRootImpl viewRoot = getViewRootImpl();
        if (viewRoot != null && viewRoot.isInLayout()) { …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-view

5
推荐指数
1
解决办法
3263
查看次数

标签 统计

android ×1

android-layout ×1

android-view ×1