android控件显示顺序控制

来源:互联网 发布:pop3协议默认端口号 编辑:程序博客网 时间:2024/06/05 05:05

在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()来设置指定控件的顺序,否则后加载的控件还是可能覆盖你想要上提的控件的。

这样我们就可以通过这个方法来排列子控件的覆盖顺序啦。

2 0
原创粉丝点击