Placing/Overlapping(z-index) a view above another view in android

前端 未结 11 1574
遥遥无期
遥遥无期 2020-11-22 17:07

I have a linear layout which consists of imageview and textview , one below another in a linear layout.



        
相关标签:
11条回答
  • 2020-11-22 17:49

    I use this, if you want only one view to be bring to front when needed:

    containerView.bringChildToFront(topView);
    

    containerView is container of views to be sorted, topView is view which i want to have as top most in container.

    for multiple views to arrange is about to use setChildrenDrawingOrderEnabled(true) and overriding getChildDrawingOrder(int childCount, int i) as mentioned above.

    0 讨论(0)
  • 2020-11-22 17:50

    RelativeLayout works the same way, the last image in the relative layout wins.

    0 讨论(0)
  • 2020-11-22 17:53

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

    0 讨论(0)
  • 2020-11-22 17:55

    Try this in a RelativeLayout:

    ImageView image = new ImageView(this);
    image.SetZ(float z);
    

    It works for me.

    0 讨论(0)
  • 2020-11-22 17:58

    Changing the draw order

    An alternative is to change the order in which the views are drawn by the parent. You can enable this feature from ViewGroup by calling setChildrenDrawingOrderEnabled(true) and overriding getChildDrawingOrder(int childCount, int i).

    Example:

    /**
     * Example Layout that changes draw order of a FrameLayout
     */
    public class OrderLayout extends FrameLayout {
    
        private static final int[][] DRAW_ORDERS = new int[][]{
                {0, 1, 2},
                {2, 1, 0},
                {1, 2, 0}
        };
    
        private int currentOrder;
    
        public OrderLayout(Context context) {
            super(context);
            setChildrenDrawingOrderEnabled(true);
        }
    
        public void setDrawOrder(int order) {
            currentOrder = order;
            invalidate();
        }
    
        @Override
        protected int getChildDrawingOrder(int childCount, int i) {
            return DRAW_ORDERS[currentOrder][i];
        }
    }
    

    Output:

    Calling OrderLayout#setDrawOrder(int) with 0-1-2 results in:

    0 讨论(0)
提交回复
热议问题