力onLayout在一个子视图,其尺寸不会改变 [英] force onLayout in a subView whose dimensions doesn't change

查看:164
本文介绍了力onLayout在一个子视图,其尺寸不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的ViewGroup 里面的一些看法。

I have a Custom ViewGroup with some views inside.

在响应事件时,一个requestLayout被称为和 OnLayout 的一些看法会
得到一个新的布局位置。

In response to an event, a requestLayout is called and in OnLayout some of the views will get a new layout position.

其中一个保持其地位,但需要重新安排里面。然而这种观点的onlayout方法不会被调用,因为它的布局位置不会改变。试图调用 requestLayout forceLayout 无效在此视图不工作。

One of them maintain its position but needs to be rearranged inside. However this view's onlayout method will not be called since its layout position doesn't change. Trying to call requestLayout, forceLayout or invalidate in this view doesn't work.

这是我发现的唯一的使坏是改变它的一些价值立场,迫使布局的变化,但我觉得应该有一个更好的办法。所以,现在我做了什么(可怕的),如:

The only dirty trick that I have found is to change some of its value position to force the layout change, but I think there should be a better way. So, by now I do something (horrible) like:

    int patch = 0;
    @Override protected void onLayout(boolean changed, int l, int t, int r, int b) {
        ...
        _myView.patchedLayout(patch);
        _myview.layout(l1, t1 , r1, t1+patch);
        patch = (patch+1) % 2;
        ...
    }       

任何方式得到相同的结果以更好的方式?

Any way to get the same result in a better way?

推荐答案

我终于得到了解决办法:我需要重写 onMeasure ,并确保调用 mesure 在我看来:

I finally got the solution: I need to override onMeasure and be sure to call mesure in my view:

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

这将设置LAYOUT_REQUIRED标志在视图的私有字段mPrivateFlags所以这将迫使调用 onLayout

这篇关于力onLayout在一个子视图,其尺寸不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆