我正在尝试从底部向单击按钮时出现的 fragment 添加动画。
fragment 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/wallet_holo_blue_light"
android:layout_gravity="center"
android:text="This is a fragmentt layout"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
向上滑动.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime">
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="50.0%p"
android:interpolator="@android:anim/decelerate_interpolator"
android:toYDelta="30" />
</set>
在这里调用:
public void onClick(View view) {
if(view == button ){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOnMap hello = new FragmentOnMap();
fragmentTransaction.add(R.id.fragment_container, hello, "HELLO");
fragmentTransaction.setCustomAnimations(R.animator.slide_up,0);
fragmentTransaction.commit();
}
}
Android Fragments and animation显示如何向上和向下滑动,我只是将 fragment 设置为动画。因此我的问题是 setcustomanimation() 函数的第二个参数
我尝试在提交之前使用 fragmentTransaction.setCustomAnimations()
,但没有帮助。
它确实出现在底部,但没有过渡效果。 任何指导都会很有用。 谢谢
最佳答案
你应该在 add
之前 setCustomAnimation
,仅此而已。
此外,您的代码应该有问题,因为您应该使用 fragment (v4) 的支持库而不是应用程序,并且在使用 fragment 时调用 getSupportFragmentManager 并修复所有代码部分,而您不使用支持库。
如果你不想改变这个,你可以把这个代码用于slide_up动画:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="translationY"
android:valueType="floatType"
android:valueFrom="1280"
android:valueTo="0"
android:duration="@android:integer/config_mediumAnimTime"/>
关于安卓 : How to slide up a fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40201915/