Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 强制重新布局视图组,包括其所有子项大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在编写一个可以放大的自定义布局(扩展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));
}

我正在使用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()) {
            if (!viewRoot.requestLayoutDuringLayout(this)) {
                return;
            }
        }
        mAttachInfo.mViewrequesTingLayout = this;
    }

我如何强迫孩子们在他们的父母上调用requestLayout时再次测量自己?

最佳答案
这将强制重新布局视图的子节点(假设视图自身的宽度和高度不需要更改)

private static void relayoutChildren(View view) {
    view.measure(
        View.MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(),View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(view.getMeasuredHeight(),View.MeasureSpec.EXACTLY));
    view.layout(view.getLeft(),view.getTop(),view.getright(),view.getBottom());
}

大佬总结

以上是大佬教程为你收集整理的android – 强制重新布局视图组,包括其所有子项全部内容,希望文章能够帮你解决android – 强制重新布局视图组,包括其所有子项所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。