如何在全屏上显示对话框片段?
How to display Dialog Fragment on FullScreen?
我有一个自定义的 DialogFragment,它只显示了一半的高度。宽度是 fine.I set height="match_parent" 在我的 layout.My 根元素是 RelativeLayout。我不知道如何解决
身高问题。
这里是javaclass.
public class DefeatDialog extends DialogFragment {
public interface DialogListener {
void onDialogPositiveClick(DialogFragment dialog);
}
public DefeatDialog() {
}
DialogListener mListener;
DefeatAlertBinding binding;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (DialogListener) context;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(context.toString()
+ " must implement NoticeDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.defeat_alert, null, false);
builder.setView(view);
binding = DefeatAlertBinding.bind(view);
binding.startAgain.setOnClickListener((v -> {
mListener.onDialogPositiveClick(DefeatDialog.this);
}));
final AlertDialog dialog = builder.create();
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.show();
return dialog;
}
}
我该如何解决?我的 xml file.Here 是我的 DialogFragment 布局。它不会完整显示 screen.Also 不会显示 xml 的所有部分。
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:orientation="vertical">
<ImageView
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@drawable/line_defeat"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057"
tools:layout_editor_absoluteX="0dp" />
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/icon_defeat"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line"
app:layout_constraintVertical_bias="0.036" />
<TextView
android:id="@+id/textView_1"
android:layout_width="400dp"
android:layout_height="80dp"
android:gravity="center"
android:text="@string/defeat"
android:textColor="@color/dark"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView_2"
android:layout_width="400dp"
android:layout_height="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_1"
app:layout_constraintVertical_bias="0.131" />
<Button
android:id="@+id/start_again"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="@drawable/button_win_defeat"
android:text="@string/button_defeat_win"
android:textColor="@color/colorWhite"
android:textSize="17sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_2"
app:layout_constraintVertical_bias="0.024" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
这是我的 DialogFragment 布局。它不会完整显示 screen.Also 不会显示 xml 的所有部分。
如果你确定你的 XML 的根是 match_parent 到 match_parent,这应该可以解决你的问题,将你的 onCreateDialog 替换为下面的
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.defeat_alert, container, false);
binding = DefeatAlertBinding.bind(view);
binding.startAgain.setOnClickListener((v -> {
mListener.onDialogPositiveClick(DefeatDialog.this);
}));
return v;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
}
}
我有一个自定义的 DialogFragment,它只显示了一半的高度。宽度是 fine.I set height="match_parent" 在我的 layout.My 根元素是 RelativeLayout。我不知道如何解决 身高问题。
这里是javaclass.
public class DefeatDialog extends DialogFragment {
public interface DialogListener {
void onDialogPositiveClick(DialogFragment dialog);
}
public DefeatDialog() {
}
DialogListener mListener;
DefeatAlertBinding binding;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (DialogListener) context;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(context.toString()
+ " must implement NoticeDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.defeat_alert, null, false);
builder.setView(view);
binding = DefeatAlertBinding.bind(view);
binding.startAgain.setOnClickListener((v -> {
mListener.onDialogPositiveClick(DefeatDialog.this);
}));
final AlertDialog dialog = builder.create();
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.show();
return dialog;
}
}
我该如何解决?我的 xml file.Here 是我的 DialogFragment 布局。它不会完整显示 screen.Also 不会显示 xml 的所有部分。
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:orientation="vertical">
<ImageView
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@drawable/line_defeat"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057"
tools:layout_editor_absoluteX="0dp" />
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/icon_defeat"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line"
app:layout_constraintVertical_bias="0.036" />
<TextView
android:id="@+id/textView_1"
android:layout_width="400dp"
android:layout_height="80dp"
android:gravity="center"
android:text="@string/defeat"
android:textColor="@color/dark"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView_2"
android:layout_width="400dp"
android:layout_height="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_1"
app:layout_constraintVertical_bias="0.131" />
<Button
android:id="@+id/start_again"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="@drawable/button_win_defeat"
android:text="@string/button_defeat_win"
android:textColor="@color/colorWhite"
android:textSize="17sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_2"
app:layout_constraintVertical_bias="0.024" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
这是我的 DialogFragment 布局。它不会完整显示 screen.Also 不会显示 xml 的所有部分。
如果你确定你的 XML 的根是 match_parent 到 match_parent,这应该可以解决你的问题,将你的 onCreateDialog 替换为下面的
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.defeat_alert, container, false);
binding = DefeatAlertBinding.bind(view);
binding.startAgain.setOnClickListener((v -> {
mListener.onDialogPositiveClick(DefeatDialog.this);
}));
return v;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
}
}