Android PopupWindow

2018-08-13  本文已影响0人  潜心之力

显示PopupWindow

    private void showShareWindow(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View child = inflater.inflate(R.layout.pop_comment_share, null);
        PopupWindow window = new PopupWindow(child, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        AutoUtils.autoSize(child);
        RelativeLayout parent = findViewById(R.id.act_comment_rl);
        window.setBackgroundDrawable(new BitmapDrawable());
        window.setOutsideTouchable(true);
        window.setFocusable(true);
        window.setAnimationStyle(R.style.CommentShareWindowTheme);
        window.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        setBackGroundAlpha(0.3f);
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackGroundAlpha(1f);
            }
        });
    }

设置弹窗外的背景颜色

    private void setBackGroundAlpha(float alpha) {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.alpha = alpha;
        getWindow().setAttributes(params);
    }

弹窗进入和退出的动画

    <style name="CommentShareWindowTheme" parent="AppTheme">
        <!-- 指定显示的动画xml -->
        <item name="android:windowEnterAnimation">@anim/act_bottom_in</item>
        <!-- 指定消失的动画xml -->
        <item name="android:windowExitAnimation">@anim/act_bottom_out</item>
    </style>

进入动画

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fillAfter="true"
    android:fromYDelta="100%"
    android:toYDelta="0" />

退出动画

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromYDelta="0"
    android:toYDelta="100%"
    android:fillAfter="true"/>
上一篇下一篇

猜你喜欢

热点阅读