Android 全屏DialogFragment
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11887340/
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
Full screen DialogFragment
提问by invertigo
I am trying to create a DialogFragment with a width of MATCH_PARENT so the dialog is nearly full screen (leaving the padding around the edges for the floating look). I have seen this solution Full Screen DialogFragment in Androidbut am trying to avoid the hack of setting the width to 1000dp. With my current layout, whether I use FILL_PARENT or MATCH_PARENT it seems to be setting the width and height to WRAP_CONTENT.
我正在尝试创建一个宽度为 MATCH_PARENT 的 DialogFragment,因此对话框几乎是全屏的(在边缘留下填充以实现浮动外观)。我在 Android 中看到了这个解决方案Full Screen DialogFragment,但我试图避免将宽度设置为 1000dp 的问题。对于我当前的布局,无论我使用 FILL_PARENT 还是 MATCH_PARENT,它似乎都将宽度和高度设置为 WRAP_CONTENT。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
I have used this solution for Dialog (not DialogFragment) and it works as expected:
我已将此解决方案用于 Dialog(不是 DialogFragment),它按预期工作:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
回答by Attif
this is perfect for me. when extends DialogFragmentoverride onCreateView()
... impelments all logic
这对我来说是完美的。当扩展DialogFragment覆盖时onCreateView()
......实现所有逻辑
dialog make it Full Screen just override this method
对话框使其全屏只需覆盖此方法
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
// the content
final RelativeLayout root = new RelativeLayout(getActivity());
root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// creating the fullscreen dialog
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(root);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
回答by atermenji
try to switch to RelativeLayout instead of LinearLayout. It worked in my case
尝试切换到RelativeLayout 而不是LinearLayout。它在我的情况下有效
回答by evya
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE,android.R.style.Theme_Holo_Light);
}
回答by Danylo.Vus
this answer help me How to set DialogFragment's width and height?
这个答案帮助我 如何设置 DialogFragment 的宽度和高度?
int width = activity.getResources().getDisplayMetrics().widthPixels;
int height = activity.getResources().getDisplayMetrics().heightPixels;
content.setLayoutParams(new LinearLayout.LayoutParams(width, height));
回答by invertigo
The best solution I found was to set the width or minWidth to some arbitrarily large value to ensure it fills the screen. I don't like this solution, but I haven't seen anything better.
我发现的最佳解决方案是将宽度或 minWidth 设置为某个任意大的值以确保它填满屏幕。我不喜欢这个解决方案,但我没有看到更好的。
回答by LukeWaggoner
In my code, I was wanting a fullscreen dialog, but still show the notification bar at the top of the screen, so I'm using:
在我的代码中,我想要一个全屏对话框,但仍然在屏幕顶部显示通知栏,所以我正在使用:
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);
This makes it fullscreen, but I'm not using a fullscreen style so that I keep the notification bar.
这使其全屏显示,但我没有使用全屏样式,因此我保留了通知栏。
Hope it helps someone!
希望它可以帮助某人!
回答by Vaibhav Sharma
You can also do something like-
你也可以做类似的事情——
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setStyle(STYLE_NO_TITLE, android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
} else {
setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_NoActionBar);
}
super.onCreate(savedInstanceState);
}
By this you will also be able to see the status bar colored for versions >= lollipop
通过这种方式,您还可以看到版本 >= 棒棒糖的颜色状态栏
回答by Biswajit
I have answered this question with explanation this is the shortest and efficient way. Please check In this thread
我已经用解释回答了这个问题,这是最短和有效的方法。请检查在这个线程
Hope this helps :)
希望这可以帮助 :)
回答by Sattar Hummatli
On DialogFragment following works. In onCreateView()
add following
在 DialogFragment 以下作品。在onCreateView()
添加以下
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//…
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//…
return view;
}
回答by loic
To have a full screen dialog, you need use
要拥有全屏对话框,您需要使用
public class CustomDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout to use as dialog or embedded fragment
return inflater.inflate(R.layout.purchase_items, container, false);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
And to show this dialog:
并显示此对话框:
CustomDialogFragment newFragment = new CustomDialogFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity
fragmentManager.beginTransaction().add(android.R.id.content, newFragment).commit();
ATTENTION: You can't use following code to show the dialog, if not, the dialog will be shown in center of screen, not full screen.
注意:您不能使用以下代码来显示对话框,否则,对话框将显示在屏幕中央,而不是全屏。
//don't show dialog like this
newFragment.show(fragmentManager, "dialog");