android控制控制的显示顺序

在android中假设首先在xml中静态加入了一个控件,剩下的控件都是通过addView动态加入。那么假设有控件覆盖的情况(比方说使用FrameLayout或者RelativeLayout),先加入得控件就会被后加入得控件覆盖。

在View类中有这样一个方法 bringToFront 。它得凝视例如以下:

     /**

     * Change the view's z order in the tree, so it's on top of other sibling

     * views. This ordering change may affect layout, if the parent container

     * uses an order-dependent layout scheme (e.g., LinearLayout). Prior

     * to {@link android.os.Build.VERSION_CODES#KITKAT} this

     * method should be followed by calls to {@link #requestLayout()} and

     * {@link View#invalidate()} on the view's parent to force the parent to redraw

     * with the new child ordering.

     *

     * @see ViewGroup#bringChildToFront(View)

     */

我们能够看到,这种方法能够改变ViewGroup内子控件在Z轴坐标得顺序。使得当前控件在全部兄弟控件得最前面,同一时候在4.4之前得版本号,还须要它的父控件调用requestLayout()和invalidate()来又一次绘制子控件的顺序。

并且要注意的一点是,须要在全部控件都载入完之后才干调用 bringToFront()来设置指定控件的顺序,否则后载入的控件还是可能覆盖你想要上提的控件的。

这样我们就可以安排订购涵盖的这种方法,它的子控件。

版权声明:本文博主原创文章。博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/zfyouxi/p/4807383.html