DialogFragment 不显示消息
DialogFragment not displaying message
我有一个 class 看起来像这样:
public class MyDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("This is a title");
alertDialogBuilder.setMessage("This is a message");
alertDialogBuilder.setPositiveButton("Yes", this);
alertDialogBuilder.setNegativeButton("No", null);
return alertDialogBuilder.create();
}
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something
}
public void showAllowingStateLoss(FragmentManager fragmentManager) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(this, "MyDialogFragment");
ft.commitAllowingStateLoss();
}
}
并且是这样使用的:
MyDialogFragment dialog = new MyDialogFragment();
dialog.showAllowingStateLoss(fragmentManager);
问题是消息没有显示在应用程序中,它只显示:
我做错了什么?
无需添加 DialogFragment.
我们有 show()
显示对话框的方法。可能是这个问题。
MyDialogFragment dialog = new MyDialogFragment();
dialog.show(getFragmentManager(),dialog.getClass().getSimpleName());
抱歉我的英语不好。
我自己找到了解决方案。显然,该对话框使用主题中的 android:textColorPrimary
作为文本颜色,而该颜色在我的应用程序中为白色。这会产生一个包含白色背景上的白色文本的警告对话框。
因此,解决方案是在 theme.xml 中添加:
<style name="MyAndroidTheme" parent="Theme.AppCompat.Light">
<item name="android:alertDialogTheme">@style/MyAlertDialogTheme</item>
</style>
<style name="MyAlertDialogTheme" parent="MyAndroidTheme">
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:windowBackground">@android:color/white</item>
<item name="android:windowIsFloating">true</item>
</style>
我真的不知道为什么需要 android:windowIsFloating
,但是如果没有它,对话框会变成全屏。
我有一个 class 看起来像这样:
public class MyDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("This is a title");
alertDialogBuilder.setMessage("This is a message");
alertDialogBuilder.setPositiveButton("Yes", this);
alertDialogBuilder.setNegativeButton("No", null);
return alertDialogBuilder.create();
}
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something
}
public void showAllowingStateLoss(FragmentManager fragmentManager) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(this, "MyDialogFragment");
ft.commitAllowingStateLoss();
}
}
并且是这样使用的:
MyDialogFragment dialog = new MyDialogFragment();
dialog.showAllowingStateLoss(fragmentManager);
问题是消息没有显示在应用程序中,它只显示:
我做错了什么?
无需添加 DialogFragment.
我们有 show()
显示对话框的方法。可能是这个问题。
MyDialogFragment dialog = new MyDialogFragment();
dialog.show(getFragmentManager(),dialog.getClass().getSimpleName());
抱歉我的英语不好。
我自己找到了解决方案。显然,该对话框使用主题中的 android:textColorPrimary
作为文本颜色,而该颜色在我的应用程序中为白色。这会产生一个包含白色背景上的白色文本的警告对话框。
因此,解决方案是在 theme.xml 中添加:
<style name="MyAndroidTheme" parent="Theme.AppCompat.Light">
<item name="android:alertDialogTheme">@style/MyAlertDialogTheme</item>
</style>
<style name="MyAlertDialogTheme" parent="MyAndroidTheme">
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:windowBackground">@android:color/white</item>
<item name="android:windowIsFloating">true</item>
</style>
我真的不知道为什么需要 android:windowIsFloating
,但是如果没有它,对话框会变成全屏。