屏幕方向更改后 DialogFragment 内容消失

DialogFragment content disappears after screen orientation change

我目前遇到的问题是,当我将屏幕方向更改为横向模式时,DialogFragment 的全部内容都消失了。

fragment_dartboard.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dartboard_fragment"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:src="@drawable/base"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:importantForAccessibility="no"
        android:id="@+id/base" />

</androidx.constraintlayout.widget.ConstraintLayout>

DartboardDialogFragment.java

public class DartboardDialogFragment extends DialogFragment {

    ...

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dartboard, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        ConstraintLayout layout = view.findViewById(R.id.dartboard_fragment);
        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.topToTop = layout.getId();
        params.startToStart = layout.getId();

        final TextView step = new TextView(getContext());
        step.setTextColor(0xFFE4FF5A);
        step.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);
        step.setText("1");
        ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            lp.startToEnd = R.id.base;
            lp.topToTop = R.id.dartboard_fragment;
            lp.bottomToBottom = R.id.dartboard_fragment;
        }
        else {
            lp.startToStart = R.id.dartboard_fragment;
            lp.endToEnd = R.id.dartboard_fragment;
            lp.topToBottom = R.id.base;
        }
        step.setLayoutParams(lp);
        layout.addView(step);

        ...
}

纵向模式

横向模式

把线layout.addView(step)去掉,旋转后图片还​​在,但我的目的是让TextView纵向显示在底部,横向显示在右侧。

我不得不更改 ImageView 方向更改的 LayoutParams

如此变化

if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            lp.startToEnd = R.id.base;
            lp.topToTop = R.id.dartboard_fragment;
            lp.bottomToBottom = R.id.dartboard_fragment;
        }

if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            view.findViewById(R.id.base).setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
            lp.startToEnd = R.id.base;
            lp.topToTop = R.id.dartboard_fragment;
            lp.bottomToBottom = R.id.dartboard_fragment;
        }

已解决问题。