android fragment切换横屏,Android DialogFragment横竖屏切换显示问题

问题:横竖屏切换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();

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值