控制您DialogFragment
的宽度和高度的一种方法是确保其对话框尊重您视图的宽度和高度(如果它们的值为WRAP_CONTENT
.
使用ThemeOverlay.AppCompat.Dialog
实现此目的的一种简单方法是利用ThemeOverlay.AppCompat.Dialog
Android 支持库中包含的样式。
DialogFragment
与Dialog
:
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_view, null);
Dialog dialog = new Dialog(getContext(), R.style.ThemeOverlay_AppCompat_Dialog);
dialog.setContentView(view);
return dialog;
}
DialogFragment
与AlertDialog
(警告:)minHeight="48dp"
:
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.ThemeOverlay_AppCompat_Dialog);
builder.setView(view);
return builder.create();
}
您还可以ThemeOverlay.AppCompat.Dialog
在创建对话框时将其设置为默认主题,方法是将其添加到应用程序的 xml 主题中。
小心,因为许多对话框确实需要默认的最小宽度才能看起来不错。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- For Android Dialog. -->
<item name="android:dialogTheme">@style/ThemeOverlay.AppCompat.Dialog</item>
<!-- For Android AlertDialog. -->
<item name="android:alertDialogTheme">@style/ThemeOverlay.AppCompat.Dialog</item>
<!-- For AppCompat AlertDialog. -->
<item name="alertDialogTheme">@style/ThemeOverlay.AppCompat.Dialog</item>
<!-- Other attributes. -->
</style>
DialogFragment
与Dialog
, 利用android:dialogTheme
:
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_view, null);
Dialog dialog = new Dialog(getContext());
dialog.setContentView(view);
return dialog;
}
DialogFragment
with AlertDialog
, 使用android:alertDialogTheme
or alertDialogTheme
(警告: minHeight="48dp"
):
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(view);
return builder.create();
}
奖金
在较旧的 Android API 上,Dialog
s 似乎有一些宽度问题,因为它们的标题(即使你没有设置)。
如果您不想使用ThemeOverlay.AppCompat.Dialog
样式并且Dialog
不需要标题(或有自定义标题),则可能需要禁用它:
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_view, null);
Dialog dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
return dialog;
}
过时的答案,在大多数情况下不起作用
我试图让对话框尊重我的布局的宽度和高度,而不以编程方式指定固定大小。
我想到了这一点android:windowMinWidthMinor
,并android:windowMinWidthMajor
导致了问题。即使它们没有包含在 my Activity
or的主题中Dialog
,它们仍然以某种方式应用于Activity
主题。
我想出了三个可能的解决方案。
解决方案 1:创建自定义对话框主题,并在DialogFragment
.
<style name="Theme.Material.Light.Dialog.NoMinWidth" parent="android:Theme.Material.Light.Dialog">
<item name="android:windowMinWidthMinor">0dip</item>
<item name="android:windowMinWidthMajor">0dip</item>
</style>
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), R.style.Theme_Material_Light_Dialog_NoMinWidth);
}
解决方案 2:创建一个自定义主题以用于ContextThemeWrapper
对话框Context
。如果您不想创建自定义对话框主题(例如,当您想使用由 指定的主题时android:dialogTheme
),请使用此选项。
<style name="Theme.Window.NoMinWidth" parent="">
<item name="android:windowMinWidthMinor">0dip</item>
<item name="android:windowMinWidthMajor">0dip</item>
</style>
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(new ContextThemeWrapper(getActivity(), R.style.Theme_Window_NoMinWidth), getTheme());
}
解决方案 3(使用AlertDialog
):强制执行android:windowMinWidthMinor
并android:windowMinWidthMajor
进入ContextThemeWrapper
由AlertDialog$Builder
.
<style name="Theme.Window.NoMinWidth" parent="">
<item name="android:windowMinWidthMinor">0dip</item>
<item name="android:windowMinWidthMajor">0dip</item>
</style>
@Override
public final Dialog onCreateDialog(Bundle savedInstanceState) {
View view = new View(); // Inflate your view here.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
// Make sure the dialog width works as WRAP_CONTENT.
builder.getContext().getTheme().applyStyle(R.style.Theme_Window_NoMinWidth, true);
return builder.create();
}