Android: FrameLayout not respecting draw order

余生长醉 提交于 2020-01-07 02:01:07

问题


I have a FrameLayout with four SurfaceViews arranged in a 2x2 grid. The user can resize each of the views.

I'd like the views to be drawn in order of their area, with the largest view drawn first and so on. Each time a view is resized, I order the views by their area, and update the FrameLayout:

public void reorderViews() {
        PlotView child1;
        PlotView child2;
        boolean swap = false;

        for(int i = 0; i < layout.getChildCount(); i++) {
            child1 = (PlotView) layout.getChildAt(i);
            for(int j = i + 1; j < layout.getChildCount(); j++) {
                child2 = (PlotView) layout.getChildAt(j);
                if(child1.area < child2.area) {
                    layout.removeViewAt(j);
                    layout.addView(child2, i);
                }
            }
        }

    }

This works, in the sense that the FrameLayout's children array holds the views in the correct order. But after re-ordering, the views continue to draw in their original order (i.e. the order in which they were originally added).

I've tried requesting layout on the FrameLayout, on the individual child views, on the FrameLayout's parent view. I've also tried invalidating everything. I've overridden the FrameLayout's onDraw hoping to force the correct draw order. No go.

A few points: I'm using FrameLayout rather than Grid because I'd like larger views to obscure smaller views, instead of having a larger view push a smaller view aside, as is the behaviour in GridView.

In order to position the child views properly, I adjust their margins and set their gravity to 'top' (it doesn't seem to matter what value I use for gravity, just so long as gravity is set to something, margins work). Might the gravity be the issue?

Also, I'm sure there's a more efficient way to reorder the views, but there's only four total, so this works ok.


回答1:


SurfaceView widgets are not drawn using the normal Android layout mechanism. Instead, each SurfaceView gets its own, separate window. I suspect those windows are not being reordered when the views are reordered. What you're trying to do would probably work for any other kind of widget.



来源:https://stackoverflow.com/questions/18602006/android-framelayout-not-respecting-draw-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!