Android5.0后的ViewGroup添加子View问题

前些日子我一直被这个问题困扰,就是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=19的显示情况,正常

API=21的显示情况,非正常

真的吓到宝宝了,在API=21以上的时候,SingleView和ViewGroup都添加的时候,ViewGroup的会下沉。

解决办法

如果是ViewGroup在添加View的时候,如果要考虑层正常问题,必须确保你添加的View时同一种类型,同时为SingleView或者同时为ViewGroup,要不就会出错啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值