Android 如何使DialogFragment宽度填充父对象
我正在开发一个android应用程序,使用Android 如何使DialogFragment宽度填充父对象,android,android-layout,android-fragments,android-dialogfragment,Android,Android Layout,Android Fragments,Android Dialogfragment,我正在开发一个android应用程序,使用DialogFragment显示对话框,但其宽度非常小。如何将此宽度设置为fill\u parent public class AddNoteDialogFragment extends DialogFragment { public AddNoteDialogFragment() { // Empty constructor required for DialogFragment } @Override
DialogFragment
显示对话框,但其宽度非常小。如何将此宽度设置为fill\u parent
public class AddNoteDialogFragment extends DialogFragment {
public AddNoteDialogFragment() {
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().setTitle(getString(R.string.app_name));
View view = inflater.inflate(R.layout.fragment_add_note_dialog,
container);
return view;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
片段添加注释对话框.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<EditText
android:id="@+id/addNoteEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="@string/clock_enter_add_note"
android:imeOptions="actionDone"
android:inputType="textCapSentences|textMultiLine"
android:lines="5" />
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/login_button"
android:text="@string/submit_button"
android:textColor="@android:color/white" />
</LinearLayout>
你有没有试过使用猫王的答案 具体如下:
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
<style name="Dialog.NoTitle" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:padding">0dp</item>
<item name="android:windowBackground">@color/window_bg</item>
</style>
public static DialogFragment createNoTitleDlg() {
DialogFragment frag = new Some_Dialog_Frag();
frag.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_NoTitle);
return frag;
}
更新:
上面的代码应该添加到
onStart()
对话框片段的方法中
这是我为处理此问题而想出的解决方案:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
编辑:
在返回膨胀视图之前,可以在onCrateView
Fragment方法中使用下面的代码
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
在我的案例中,我还使用了以下方法:
@Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.dialog_height));
}
}
但在某些棒棒糖+设备(如Nexus 9)上,对话框的左右边缘与屏幕边缘之间仍有很小的间隙
这并不明显,但最后我发现,要使其在所有设备和平台上都全宽,应在style.xml中指定窗口背景,如下所示:
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
<style name="Dialog.NoTitle" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:padding">0dp</item>
<item name="android:windowBackground">@color/window_bg</item>
</style>
public static DialogFragment createNoTitleDlg() {
DialogFragment frag = new Some_Dialog_Frag();
frag.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_NoTitle);
return frag;
}
在我的对话框布局的顶部完成了这个技巧。在DialogFragment的onCreateView方法中使用这个
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **220**, getResources().getDisplayMetrics());
getDialog().getWindow().setLayout(width,px);
220是对话框碎片高度,可以随意更改它这对我来说很有用
创建自定义样式:
<style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowMinWidthMajor">97%</item>
<item name="android:windowMinWidthMinor">97%</item>
</style>
DialogFragment中的用户AlertDialog.Builder。像这样将其添加到
onCreateDialog
方法中。在onCreateView
中,什么也不做
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
View view = LayoutInflater.from(getContext()).inflate(R.layout.gf_confirm_order_timeout_dialog, null);
final Bundle args = getArguments();
String message = args.getString(GfConstant.EXTRA_DATA);
TextView txtMessage = (TextView) view.findViewById(R.id.txt_message);
txtMessage.setText(message);
view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (mListener != null)
{
mListener.onDialogConfirmOK();
}
}
});
builder.setView(view);
Dialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
对我来说,当我用RelativeLayout替换onCreateView中膨胀的布局的LinearLayout父级时,它起到了作用。不需要其他代码更改
public class FullWidthDialogFragment extends AppCompatDialogFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_AppCompat_Light_Dialog_Alert);
}
}
如果您想要更大的灵活性,可以扩展AppCompat对话框警报和自定义属性在style.xml文件中创建自定义样式。只需将此代码复制粘贴到style.xml文件中
<style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
<item name="android:windowBackground">@null</item>
<item name="android:windowIsFloating">true</item>
</style>
这对我很有用,希望这也能帮助你在布局根目录中,将android:minWidth设置为一个非常大的值 e、 g
...
另一种对我有效且无副作用的解决方案是:
在扩展DialogFragment的类中:
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
}
以您的风格:
<style name="DialogStyle" parent="ThemeOverlay.AppCompat.Dialog.Alert"></style>
我想澄清这一点。这两种方法都是正确的,但是使用不同的DialogFragment 使用
android.app.DialogFragment
使用android.support.v4.app.DialogFragment
基于其他解决方案,我创建了自己的解决方案
<style name="AppTheme.Dialog.Custom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowNoTitle">true</item>
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
</style>
当我面对这个问题时,我就是这样解决的。试试这个 在对话框片段的onActivityCreated中,根据需要添加此代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
myview=inflater.inflate(R.layout.fragment_insurance_detail, container, false);
intitviews();
return myview;
}
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
getDialog().getWindow().setGravity(Gravity.CENTER);
}
这对我很有效
android:minWidth="300dp"
通过这一点,您可以为对话框片段提供宽度。我尝试了上面列出的多个答案;他们不为我工作。 这就是我的问题的解决方案: 在
对话框片段()
中,添加以下代码:
override fun onResume() {
super.onResume()
if (showsDialog) {
val width = ViewGroup.LayoutParams.MATCH_PARENT
val height = ViewGroup.LayoutParams.WRAP_CONTENT
dialog?.window?.apply {
setBackgroundDrawable(ColorDrawable(Color.WHITE))
attributes.gravity = Gravity.BOTTOM
setLayout(width, height)
}
}
}
备选案文1。在oncreate in活动中添加以下代码
也可以为对话框创建自定义样式
或者您可以尝试下面的代码样式将帮助您
如果将
ConstraintLayout
用作根布局,而不使用任何其他代码,则它将变为全宽
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.constraintlayout.widget.ConstraintLayout>
一种更清晰、更灵活的方法是设置屏幕宽度的百分比。同样的高度也可以,而且很容易改变
override fun onResume() {
super.onResume()
dialog?.window?.let { window ->
val params: ViewGroup.LayoutParams = window.attributes
params.width = context?.getScreenSize(false)?.x?.times(0.9)?.toInt()
?: ViewGroup.LayoutParams.MATCH_PARENT
params.height = ViewGroup.LayoutParams.WRAP_CONTENT
window.attributes = params as WindowManager.LayoutParams
}
}
您导入了哪些布局参数?@ymerdrengene实际上是个很好的问题。尝试
WindowManager.LayoutParams.FILL\u PARENT
@ymerdrengene尽管从技术上讲,它们都只是从ViewGroup.LayoutParams
继承了这个值,所以无论哪种方式它都能很好地工作。正如这里提到的,我建议将此代码移到onCreateDialog
导入android.view.WindowManager;实际上,使用RelativeLayout的完全相同的布局显示得非常好。不过我没有解释…您可以调用dialog.getWindow().setBackgroundDrawable(null)代码>如果设置为null,则它不是透明的。。。只有黑色,在4.0.4中对我来说,如果我放置getDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.MATCH_父级),它就可以工作在对话框fragment的onResume()方法中的code>。它在onCreateView()
方法上不起作用。我在onStart
方法中将答案从dialog
略微修改为getDialog()
setLayout
。谢谢。这应该是公认的答案!节省了我这么多时间!窗口背景起到了作用。这对我来说很有效,我喜欢它不需要我们在代码中添加任何额外的内容,但我想知道为什么它有效……难以置信的是,这是唯一对我有效的东西。定制风格并不完美。需要像这样简单的东西。典型的,总是最不受欢迎的关于堆栈溢出的答案解决了我的问题。ThanksI非常喜欢这个答案,但不知道为什么它只适用于相对布局?我必须在样式中添加以下内容以使其适用于我:100%100%beautiful solution with@null。注意使用正确的父项来匹配其他对话框。在我的例子中,是ThemeOverlay.AppCompat.dialogy,基本上我需要全宽,这很有效。谢谢您可以直接在setStyle()中使用警报主题
android:minWidth="300dp"
override fun onResume() {
super.onResume()
if (showsDialog) {
val width = ViewGroup.LayoutParams.MATCH_PARENT
val height = ViewGroup.LayoutParams.WRAP_CONTENT
dialog?.window?.apply {
setBackgroundDrawable(ColorDrawable(Color.WHITE))
attributes.gravity = Gravity.BOTTOM
setLayout(width, height)
}
}
}
getDialog().getWindow()
.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
<style name="CustomDialog" parent="AppTheme" >
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowCloseOnTouchOutside">true</item>
</style>
then use that style in dialog fragment
@Override public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
}
@Override public void onStart() {
super.onStart();
getDialog().getWindow()
.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT);
}
SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
SampleDialogFragment.show(getActivity().getSupportFragmentManager(), "sometag");
<item name="android:windowBackground">@null</item>
class TestDialog : DialogFragment() {
override fun onStart() {
super.onStart()
dialog?.window?.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
}
}
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.constraintlayout.widget.ConstraintLayout>
override fun onResume() {
super.onResume()
dialog?.window?.let { window ->
val params: ViewGroup.LayoutParams = window.attributes
params.width = context?.getScreenSize(false)?.x?.times(0.9)?.toInt()
?: ViewGroup.LayoutParams.MATCH_PARENT
params.height = ViewGroup.LayoutParams.WRAP_CONTENT
window.attributes = params as WindowManager.LayoutParams
}
}