【问题标题】:How to make dialog to slide up and exit from current position?如何使对话框向上滑动并从当前位置退出?
【发布时间】:2015-10-31 21:15:45
【问题描述】:
我需要将对话框从当前位置翻译到顶部作为退出动画。如果有键盘,它将在顶部可用,并且应该从该位置平移并退出,以防如果键盘不可见,它将位于中心并应该从那里退出。
试过下面的sn-p:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillBefore="true"
android:fillEnabled="true">
<translate
android:duration="400"
android:toYDelta="-100%p"
/>
</set>
在这种情况下,当对话框不在中心时(当键盘可见时),它会下降一次(到其中心位置)然后向上平移。这会导致闪烁。我究竟做错了什么?如何顺利翻译
【问题讨论】:
标签:
android
android-layout
android-dialog
【解决方案1】:
slideup.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1"
android:toAlpha="0"
android:duration="500"/>
<translate android:fromYDelta="0%" android:toYDelta="-100%" android:duration="500"/>
</set>
style.xml:
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowAnimationStyle">@style/MyAnimation.Window</item>
</style>
<style name="MyAnimation.Window" parent="@android:style/Animation.Activity">
<item name="android:windowExitAnimation">@anim/slide_up</item>
</style>
在你的代码上
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, R.style.DialogTheme);
}
【解决方案2】:
您可以使用下面的代码来上下显示自定义对话框。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="0%p"
android:toYDelta="100%p" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="50%p"
android:toYDelta="0%p" />
</set>