主要代码就是Dialog的style,而且windowAnimationStyle可以设置两次。
@Override public void onActivityCreated(Bundle savedInstanceState) {
Window window = getDialog().getWindow();
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
attributes.dimAmount = 0.8f;
attributes.gravity = Gravity.BOTTOM;
window.setAttributes(attributes);
super.onActivityCreated(savedInstanceState);
}
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(), R.style.Dialog_Fullscreen_Bottom);
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
<!--dialog从底部弹出-->
<style name="Dialog_Fullscreen.Bottom">
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="android:windowAnimationStyle">@style/bottomDialogStyle</item>
</style>
<style name="Dialog_Fullscreen">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
<!-- 背景是否变灰 -->
<item name="android:windowAnimationStyle">@style/centerDialogStyle</item>
<!-- 背景动画-->
<item name="android:windowFrame">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:indeterminateOnly">true</item>
<item name="android:windowIsFloating">true</item>
</style>
<style name="centerDialogStyle">
<item name="android:windowEnterAnimation">@android:anim/fade_in</item>
<item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>
<style name="bottomDialogStyle">
<item name="android:windowEnterAnimation">@anim/anim_bottom_dialog_in</item>
<item name="android:windowExitAnimation">@anim/anim_bottom_dialog_out</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
>
<translate
android:duration="250"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="100%"
android:toXDelta="0"
android:toYDelta="0">
</translate>
<alpha
android:duration="200"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
</set>
anim_bottom_dialog_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:duration="250"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="100%">
</translate>
<alpha
android:duration="200"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
anim_bottom_dialog_out.xml
Activity设置为从底部弹出。
private void initAnim() {
// 解决底部弹出动画会有黑线,https://blog.csdn.net/abc6368765/article/details/53064889
Window win = this.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.BOTTOM; // 修改当前activity or dialogfragment不紧挨底部问题
lp.dimAmount = 0.8f; // 这是没有布局的地方可以显示为黑色背影
win.setAttributes(lp);
overridePendingTransition(R.anim.push_bottom_in, R.anim.push_bottom_out);
}
@Override public void finish() {
super.finish();
overridePendingTransition(R.anim.push_bottom_in, R.anim.push_bottom_out);
}