安卓子view的前后关系,z轴效果更改的一些思路
生活随笔
收集整理的這篇文章主要介紹了
安卓子view的前后关系,z轴效果更改的一些思路
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我想了幾種解決方法:
1、將removeAllViews()移除所有子view,然后再按需要的順序add進去
2、安卓自帶了bringTofrount()可以將一個子View移動到最前, 其中的bringTofrount()只能把子view移動到最前面,不能任意改變view的前后關系, 可以不同子view多次調用這個方法 3、安卓5.0之后的view有一個z軸屬性,可以關注一下,4、設置兩套view其中一套是鏡像,也就是只用來顯示的,根據要求進行setVisibility(INVISIBLE)或者setVisibility(VISIBLE);
著重看一下bringTofrount()這種方式
其實bringTofrount()方法是調用了ViewGroup中的
bringChildToFront(),繼續向父類追蹤會發現其實現原理 public void bringChildToFront(View child) {final int index = indexOfChild(child);if (index >= 0) { removeFromArray(index);addInArray(child, mChildrenCount);child.mParent = this;requestLayout();invalidate();} } 注意到removeFromArray(index);的實現如下private void removeFromArray(int index) {final View[] children = mChildren;if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {children[index].mParent = null;}final int count = mChildrenCount;if (index == count - 1) {children[--mChildrenCount] = null;} else if (index >= 0 && index < count) {System.arraycopy(children, index + 1, children, index, count - index - 1);children[--mChildrenCount] = null;} else {throw new IndexOutOfBoundsException();}if (mLastTouchDownIndex == index) {mLastTouchDownTime = 0;mLastTouchDownIndex = -1;} else if (mLastTouchDownIndex > index) {mLastTouchDownIndex--;} } 其中這句final View[] children = mChildren;引用了mChildren這個成員變量,而該成員變量在ViewGroup中的申明是私有的,也就是說無法通過子類來進行操作private View[] mChildren;曾經看過有人通過反射機制調用安卓內部Hidden的函數,待閑余時間再試 另外更改view的前后關系對于像幀布局或者相對布局一般不會打亂布局的橫向關系,但是對于線性布局則會完全打亂這種結構,這個必須要注意總結
以上是生活随笔為你收集整理的安卓子view的前后关系,z轴效果更改的一些思路的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于Redis ZSet结构实现朋友圈点
- 下一篇: UI设计细分为哪些具体职位?国内提供了哪