美文网首页
Android DialogFragment横竖屏切换显示问题

Android DialogFragment横竖屏切换显示问题

作者: jxtx | 来源:发表于2020-07-29 16:51 被阅读0次

问题:横竖屏切换fragment宽高不适应,没有及时改变
解决方法:
1.在manifest文件中对应的activity添加下面代码,防止重复创建多个fragment

android:configChanges="orientation|screenSize|keyboardHidden"

2.添加完上面代码发现只会创建一次,需要添加横竖屏切换监听

public boolean isDialogFragmentShowing() {
        if(privacyAgreementDialogFragment!= null && privacyAgreementDialogFragment.getDialog() != null && privacyAgreementDialogFragment.getDialog().isShowing()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
//            Toast.makeText(getApplicationContext(), "横屏", Toast.LENGTH_SHORT).show();
            if (isDialogFragmentShowing()){
                privacyAgreementDialogFragment.dismiss();
                privacyAgreementDialogFragment=null;
            }else {
                return;
            }
            if (privacyAgreementDialogFragment==null){
                privacyAgreementDialogFragment = PrivacyAgreementDialogFragment.newInstance();
            }
            privacyAgreementDialogFragment.show(getSupportFragmentManager(),"PrivacyAgreement");
        }else{
//            Toast.makeText(getApplicationContext(), "竖屏", Toast.LENGTH_SHORT).show();
            if (isDialogFragmentShowing()){
                privacyAgreementDialogFragment.dismiss();
                privacyAgreementDialogFragment=null;
            }else {
                return;
            }
            if (privacyAgreementDialogFragment==null){
                privacyAgreementDialogFragment = PrivacyAgreementDialogFragment.newInstance();
            }
            privacyAgreementDialogFragment.show(getSupportFragmentManager(),"PrivacyAgreement");
        }
    }

3.最后记得在onCreate中调用

if (privacyAgreementDialogFragment==null){
            privacyAgreementDialogFragment = PrivacyAgreementDialogFragment.newInstance();
        }
        privacyAgreementDialogFragment.show(getSupportFragmentManager(),"PrivacyAgreement");
public class PrivacyAgreementDialogFragment  extends BaseDialogFragment{


    public static PrivacyAgreementDialogFragment newInstance() {

        Bundle args = new Bundle();

        PrivacyAgreementDialogFragment fragment = new PrivacyAgreementDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_privacy_agreement;
    }



    @Override
    public void onResume() {
        super.onResume();
        CanceledOnTouchOutside();

        if (getDialog() != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            int widthPixel = dm.widthPixels;
            int heightPixel = dm.heightPixels;

//            getDialog().getWindow().setLayout(DensityUtil.dip2px(getContext(),252), DensityUtil.dip2px(getContext(),223));
            getDialog().getWindow().setLayout(widthPixel*2/3, heightPixel*2/3);
        }
    }

    WebView webview;
    TextView tv_cancel,tv_confirm;
    @Override
    public void initView(View view) {
        webview=view.findViewById(R.id.webview);
        tv_cancel=view.findViewById(R.id.tv_cancel);
        tv_confirm=view.findViewById(R.id.tv_confirm);

    }

    @Override
    public void initData() {
        WebViewSettingUtil.generalSetting(webview);
        String url="";
        webview.loadUrl(url);
        //如果不设置WebViewClient,请求会跳转系统浏览器
        webview.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webview.loadUrl(url);
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                tvTitle.setText(view.getTitle());
            }
        });

        //        //防止中文乱码
        webview.getSettings().setDefaultTextEncodingName("UTF -8");
        webview.getSettings().setJavaScriptEnabled(true);


    }


    @Override
    public void initListener() {
        tv_cancel.setOnClickListener(v -> {
            dismiss();
            System.exit(0);

        });
        tv_confirm.setOnClickListener(v -> {
            dismiss();
        });

    }
}

相关文章

网友评论

      本文标题:Android DialogFragment横竖屏切换显示问题

      本文链接:https://www.haomeiwen.com/subject/liqkrktx.html