Android 如何使 DialogFragment 宽度为 Fill_Parent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23990726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make DialogFragment width to Fill_Parent
提问by N Sharma
I am working on an android application where I am using DialogFragment
to display the dialog but its width is very small. How I can make this width to fill_parent
to it ?
我正在开发一个DialogFragment
用于显示对话框的 android 应用程序,但它的宽度非常小。我怎样才能使这个宽度fill_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;
}
}
fragment_add_note_dialog.xml
fragment_add_note_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>
回答by savepopulation
This is the solution I figured out to handle this issue:
这是我想出的解决此问题的解决方案:
@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));
}
}
Edit:
编辑:
You can use the code below in onCrateView
method of Fragment before return the inflated view.
您可以onCrateView
在返回膨胀视图之前在Fragment 方法中使用以下代码。
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
回答by EpicPandaForce
Have you tried using the answer of Elvis from How to make an alert dialog fill 90% of screen size?
您是否尝试过使用 Elvis 从How to make an alert dialog fill 90% of screen size 中的答案?
It is the following:
它是以下内容:
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Update:
更新:
Above code should be added inside onStart()
method of the DialogFragment
.
上面的代码应该添加onStart()
到DialogFragment
.
LayoutParams.FILL_PARENT
is deprecated use LayoutParams.MATCH_PARENT
instead.
LayoutParams.FILL_PARENT
已弃用LayoutParams.MATCH_PARENT
。
回答by Kishan Vaghela
This is worked for me
这对我有用
Create your custom style :
创建您的自定义样式:
<style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowMinWidthMajor">97%</item>
<item name="android:windowMinWidthMinor">97%</item>
</style>
Use this style in your dialog
在对话框中使用此样式
public class MessageDialog extends DialogFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
}
// your code
}
回答by AA_PV
For me, it worked when I replaced the LinearLayout parent of the layout inflated in onCreateView by RelativeLayout. No other code change required.
对我来说,当我用 RelativeLayout 替换在 onCreateView 中膨胀的布局的 LinearLayout 父级时,它起作用了。无需其他代码更改。
回答by RJ M
<!-- A blank view to force the layout to be full-width -->
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_gravity="fill_horizontal" />
in the top of my dialog layout did the trick.
在我的对话框布局的顶部做到了这一点。
回答by Mehrdad Faraji
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);
}
}
and if you want much more flexibility can extend AppCompat_Dialog_Alert and custom attributes
如果你想要更多的灵活性可以扩展 AppCompat_Dialog_Alert 和自定义属性
回答by goRGon
In my case I also used the following approach:
就我而言,我还使用了以下方法:
@Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.dialog_height));
}
}
But there were still small gapsbetween left and right edges of the dialog and the screen edges on some Lollipop+ devices (e.g. Nexus 9).
但是在某些 Lollipop+ 设备(例如 Nexus 9)上,对话框的左右边缘与屏幕边缘之间仍然存在小间隙。
It was not obvious but finally I figured out that to make it full widthacross all the devices and platforms window backgroundshould be specified inside styles.xmllike the following:
这并不明显,但最后我发现要使其在所有设备和平台上全宽,窗口背景应该在style.xml 中指定,如下所示:
<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>
And of course this style needs to be used when we create the dialog like the following:
当然,当我们创建对话框时需要使用这种样式,如下所示:
public static DialogFragment createNoTitleDlg() {
DialogFragment frag = new Some_Dialog_Frag();
frag.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_NoTitle);
return frag;
}
回答by Khemraj
I want clarify this. Both the ways are right, But with different DialogFragment.
我想澄清这一点。两种方式都是对的,但使用不同的 DialogFragment。
Using android.app.DialogFragment
使用 android.app.DialogFragment
@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);
}
}
Using android.support.v4.app.DialogFragment
使用 android.support.v4.app.DialogFragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
回答by Владислав Стариков
Based on other solutions, I have created my own.
基于其他解决方案,我创建了自己的解决方案。
<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>
abstract class BaseDialogFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.AppTheme_Dialog_Custom)
}
}
回答by Josiel Novaes
Another solution that work for me without side effects was:
另一个对我没有副作用的解决方案是:
In your class that extends DialogFragment:
在您扩展 DialogFragment 的类中:
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
}
In your styles:
在你的风格中:
<style name="DialogStyle" parent="ThemeOverlay.AppCompat.Dialog.Alert"></style>