前些日子我一直被这个问题困扰,就是ViewGroup调用addView的ChildView的绘制层问题。我之前在CSDN上进行了提问,可惜没有人回答我。最近,我又想起了这个问题,我又进行精简探索。之前的问题的地址:http://ask.csdn.net/questions/376400
现象
我在Layout文件下编写以下代码:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_view_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.owant.test.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:background="#ff0000"
android:text="view1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="view2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="view2"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:text="view3"/>
</FrameLayout>
相信大家按照以前的思维,他们的显示层是:View1》View2》View3
但是情况有点诡异,如下图:
真的吓到宝宝了,在API=21以上的时候,SingleView和ViewGroup都添加的时候,ViewGroup的会下沉。
解决办法
如果是ViewGroup在添加View的时候,如果要考虑层正常问题,必须确保你添加的View时同一种类型,同时为SingleView或者同时为ViewGroup,要不就会出错啦。