控制您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
(caveat:)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
与一起AlertDialog
使用,android:alertDialogTheme
或使用或alertDialogTheme
(caveat:)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
或主题中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();
}