按下"返回" android片段上的按钮

时间:2014-03-19 21:24:34

标签: android android-fragments android-animation back-stack

我正在尝试创建动画,当用户按下"返回"在他的Android设备上。 我想回到上一个片段并通过动画从上到下关闭实际片段以关闭片段。

以下是我启动片段时的代码:

FragmentTransaction fragmentTransaction = getActivity().getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_right, R.animator.up_from_bottom);
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

以下是R.animator.up_from_bottom.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="@android:anim/decelerate_interpolator">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="100%" android:toYDelta="0%"
        android:duration="400" />
</set>

以下是例外:

03-19 22:20:38.498: E/InputEventSender(15279): Exception dispatching finished signal.
03-19 22:20:38.498: E/MessageQueue-JNI(15279): Exception in MessageQueue callback: handleReceiveCallback
03-19 22:20:38.498: E/MessageQueue-JNI(15279): java.lang.RuntimeException: Unknown animator name: translate
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:129)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:126)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:93)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java:72)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.app.FragmentManagerImpl.loadAnimator(FragmentManager.java:743)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1167)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:717)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1496)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:495)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.app.Activity.onBackPressed(Activity.java:2215)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.app.Activity.onKeyUp(Activity.java:2193)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
03-19 22:20:38.498: E/MessageQueue-JNI(15279):  at android.app.Activity.dispatchKeyEvent(Activity.java:2423)

1 个答案:

答案 0 :(得分:1)

使用setCustomAnimation使用标准Fragment(而非支持库)时,您需要使用R.animator代替R.anim