How to bring ImageView in front of Button in android 5?

前端 未结 2 1301
执念已碎
执念已碎 2020-12-10 12:39

In android versions previous to lolipop, the following code works and an image is in front of the button. But in android 5 the imageview is put behind the button.

         


        
相关标签:
2条回答
  • 2020-12-10 13:22

    The problem appears Android 5.0's elevation property. Apparently, the RelativeLayout Z-axis ordering is tied into elevation. If both widgets have the same elevation, the RelativeLayout will determine the Z-axis order -- you can see that if you were to switch your layout to be both Button widgets, for example. However, if one widget (Button) has an elevation, and another widget (ImageView) does not, the elevation will take precedence.

    You can remove the Button elevation via android:stateListAnimator="@null" or by defining your own custom animator. Or, you can add some elevation to your ImageView to get it to be higher on the Z axis than is the Button.

    0 讨论(0)
  • 2020-12-10 13:28

    Button elevation and translationZ values are defined in framework as below:

    <!-- Elevation when button is pressed -->
    <dimen name="button_elevation_material">1dp</dimen>
    <!-- Z translation to apply when button is pressed -->
    <dimen name="button_pressed_z_material">2dp</dimen>
    

    Source

    Like CommonsWare explained, set the translationZ of ImageView higher than Button value will give the result as expected.

    0 讨论(0)
提交回复
热议问题