Android横竖屏切换Dialog、PopWindow无法消失的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/inwhites/article/details/79247590

Android实际开发当中,经常会使用到Dialog以及PopWindow这些窗体控件,而这些控件的应用场景往往是这样的,切换横竖屏的时候,你要让它消失。其实正常情况下,在Activity中显示一个Dialog或者PopWindow的时候,切换横竖屏的时候,这些控件是会自动消失的。但是呢,如果你给Activity配置了如下属性,就会导致Dialog及PopWindow消失不了:

android:configChanges="screenSize|orientation"

这个属性设置之后,会导致Activity不会重新创建,原因就是Activity生命周期不会重新走一次(两者缺一不可)。这时候由于Activity没有销毁,在该Activity上的View以及窗体都不会销毁,Activity只会执行onConfigurationChanged方法,最后导致Dialog以及PopWindow在横竖屏切换时不会自动消失。
那么怎么解决这个问题呢?
1.监听Activity或Fragment的onConfigurationChanged方法,方向改变时自己设置让PopWindow(Dialog同理)消失。但是实际开发中,根本就没法预料你可能需要在哪个地方弹出来一个PopWindow,可能是在一个Fragment中可能是在一个自定义View中,也可能在不同的模块中,最烦的就是为传一个方向改变事件,要写好几个方法,一层一层的往下传。怎么让PopWindow自己去检测事件自己消失呢?看第二种方法。
2.我们都知道Activity以及Fragment都有监听方向改变的方法,就是onConfigurationChanged(),其实我们经常用到的View的方法也是有这个方法的,如下:

/**
 * Called when the current configuration of the resources being used
 * by the application have changed.  You can use this to decide when
 * to reload resources that can changed based on orientation and other
 * configuration characteristics.  You only need to use this if you are
 * not relying on the normal {@link android.app.Activity} mechanism of
 * recreating the activity instance upon a configuration change.
 *
 * @param newConfig The new resource configuration.
 */
protected void onConfigurationChanged(Configuration newConfig) {
}

想到这里应该就不难了,我们要显示在PopWindow上的其实就是一个个View,我们其实只需提供一个父布局容器ViewGroup,在这个父布局容器rootView中监听onConfigurationChanged,然后在方法中调用PopWindow的dismiss方法即可。代码如下

public class AutoDissOrientationChgPop {

    private Context mContext;
    private PopupWindow mPopWindow;
    private RootView mRootView; //自定义的ViewGroup,用来监听横竖屏切换


    public AutoDissOrientationChgPop(Context context) {
        mContext = context;
        mPopWindow = new PopupWindow(mContext);
        mRootView = new RootView(mContext);
        mPopWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.GRAY));
        mPopWindow.setOutsideTouchable(true);

    }


    public void setContetView(View view) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mRootView.addView(view, params);//添加要显示的View
        mPopWindow.setContentView(mRootView);//将视图放到PopWindow中
    }

    public void show(View view) {
        mPopWindow.showAtLocation(view,Gravity.CENTER,0,0);
    }


    private class RootView extends LinearLayout {

        public RootView(Context context) {
            super(context);
        }

        public RootView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }

        public RootView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        public RootView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        @Override
        protected void onConfigurationChanged(Configuration newConfig) {
            if (mPopWindow != null && mPopWindow.isShowing()) {//方向改变,pop消失
                mPopWindow.dismiss();
            }

            super.onConfigurationChanged(newConfig);
        }
    }


}

这样就可以实现PopWindow在横竖屏切换时自己自动消失了。

猜你喜欢

转载自blog.csdn.net/inwhites/article/details/79247590