问题
Hi I am developing android application in which I am trying to implement new material design features. I tried to apply both elevation property and TranslationZ property but it is not working.
<Button
android:id="@+id/button1"
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Name" />
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#0073ff" />
<corners android:radius="16dp" />
</shape>
<style name="ButtonStyle">
<item name="android:elevation">8dp</item>
<item name="android:translationZ">8dp</item>
<item name="android:background">@drawable/file</item>
</style>
回答1:
TranslationZ is a dynamic property used for animation. Basically it's needed to nicely handle elevation changes. When you press a button, its elevation remains unchanged and its translationZ is being animated. This way the View always knows what was the original elevation value and can respond correctly to multiple touch events.
Internally Z = elevation + translationZ
回答2:
Simple Fix
Your problem with losing the touch feedback & Z animations when updating the style comes from not extending the parent android:Widget.Material.Button in your style. If you do that, you won't have to do any manual animation or touch feedback. Just overwrite what you need!
Z-depth explanation
As per the documentation, TranslationZ
is just the dynamic component of the Z value. What this means is that when you animate your views in the Z-axis (with ViewPropertyAnimator
and such), the animation state will start from the static component of the Z value, the elevation
, and will end at the full Z value with with the translation
component being the change in between, or the delta.
Z = elevation + translationZ
To implement an animation with translationZ, you'd need to make a StateListAnimator
. In your particular case, because you are not animating anything, in order to set a Z-depth to your Button
, you just need elevation
.
来源:https://stackoverflow.com/questions/29848276/what-does-translationz-actually-do-in-android