How to send view to back ? How to control the z-order programmatically?

后端 未结 12 948
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 04:12

I have a problem to send the view to back. In Android we have a method like bringToFront(), to place the view on top of the another view. Like that, I want to p

相关标签:
12条回答
  • 2020-12-05 04:34

    You can also try to use functionality of ViewGroup.addViewInLayout() method which take an index, determining should the view be at the end or at the beginning of the list

    0 讨论(0)
  • 2020-12-05 04:35

    Please note that you can use view.setZ(float) starting from API level 21. Here you can find more info.

    0 讨论(0)
  • 2020-12-05 04:36

    You could try doing view.setVisibility(View.INVISIBLE) or view.setVisibility(View.GONE).

    UPDATE:

    This shows you how to accomplish what you want to do.

    0 讨论(0)
  • 2020-12-05 04:39

    There is no sendToBack() method. But if you call bringToFront() on the lower positioned view you get almost the same effect.

    0 讨论(0)
  • 2020-12-05 04:40

    Afaik there's no built-in solution for this. I guess you're trying to modify the z-order in a FrameLayout, or something similar.

    However, I think you can modify the order of the contained child elements in the layout. RemoveChild...() and addView() methods can take position values, so you could most likely swap child elements around, and that would modify the z-order. It seems a bit hacky solution however.

    Or consider modifying the visibility property of the child views, you may get similar behaviour, and that'd be much cleaner I think.

    Edit:

    With the new Android version, the L Developer Preview it seems that at last we have the ability to easily change the Z ordering of Views. 'Elevation' and 'TranslationZ' properties to the rescue: https://developer.android.com/preview/material/views-shadows.html

    0 讨论(0)
  • 2020-12-05 04:40

    I realize that this has been implied in other answers, but no one posted the code. I keep the following in a utility class where I have "helper" functions for dealing with views:

    public static void sendViewToBack(final View child) {
        final ViewGroup parent = (ViewGroup)child.getParent();
        if (null != parent) {
            parent.removeView(child);
            parent.addView(child, 0);
        }
    }
    
    0 讨论(0)
提交回复
热议问题