FrameLayout&RelativeLayout下,和Button同级的View需注意

content_margin_compat_xml_-_StatusBarCompat_-____demoPro_StatusBarCompat_.png

如上面的布局所示,在5.0以上和5.0以下会有不同的效果:

c.png

b_.png

可以看到,Button跑到了最上面,这是为毛啊?没找到答案,但找到了不是办法的解决办法,就是在Button上包一层布局即可。

后续。。

在Stack Overflow上找到了答案:
Starting with Lollipop, a StateListAnimator controlling elevation was added to the default Button style. In my experience, this forces buttons to appear above everything else regardless of placement in XML (or programmatic addition in your case). That might be what you're experiencing.
To resolve this you can add a custom StateListAnimator if you need it or simply set it to null if you don't.
XML:
android:stateListAnimator="@null"

Java:
Button button = new Button(this); button.setText("Button"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { button.setStateListAnimator(null); }

More details: Android 5.0 android:elevation Works for View, but not Button?

也就是说在5.0以上,系统会在button上wrapper上一层效果,就是StateListAnimator。去掉它或者自定义一个就可以解决上述问题。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容