Android 5.0之后的机器上面, Android Button 总是显示在FrameLayout的最前面

详情
详解

这个是因为Android 5.0之后引入了一个Material Design的概念:
https://developer.android.com/training/material/shadows-clipping.html?hl=zh-cn

视图的 Z值包含两个组件:
高度(Elevation):静态组件。Elevation
转换(Translation):用于动画的动态组件。

Z = elevation + translationZ

这里TranslationZ就是指的在Z轴上面的转换,注意转换,主要是用于动画的动态的组件,例如Button,它在按下去的时候是0dp的高度,正常的状态下是6dp的高度。所以Z = elevation + translationZ;

解决办法:

1: 给最上面的TextView设置一个TranslationZ或者是Elevation设置一个高的值,这个值要比上面测试的结果6+3=9大,就是至少10
就是上面的公式:Z = elevation + translationZ
这里TextView的Z值一定要比Button的Z值要大

android:elevation="10dp"  
android:translationZ="10dp" 

// 或者
if(Build.VERSION.SDK_INT>21) {  
        upTextView.setElevation(10);  
        upTextView.setTranslationZ(10);  
}  

2.2: 将Button的动画的StateListAnimation设置为null,因为如果将Button的StateListAnimation 取消了Translation就会变成0了

android:stateListAnimator="@null"  

你可能感兴趣的:(Android 5.0之后的机器上面, Android Button 总是显示在FrameLayout的最前面)