DialogFragment全屏显示两侧填充

ahm*_*mad 29 android android-layout android-dialogfragment

我正在创建一个显示在操作栏下方的自定义DialogFragment.到目前为止一切都很好.对话框片段的布局参数用于match_parent宽度和wrap_content高度.

我尝试了所有解决方案,包括设置布局参数.width以及获取显示大小和删除主题.然而,无论在对话框的左侧,右侧和顶侧是否有少量空间是不可见的.我需要知道如何删除这个空间,以便它实际上匹配屏幕的宽度,并希望摆脱顶部填充.

ahm*_*mad 29

我用自定义对话框主题想出来了.windowIsFloating true将摆脱背景,但会在背景下添加一些额外的空间作为背景.在这种情况下,您可以使用windowBackground @null来擦除它.

<style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
    <item name="android:windowBackground">@null</item>
    <item name="android:windowIsFloating">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

感谢Raghunandan,他给了我包含所有样式属性的链接.我花了一段时间,但我浏览了那个文件,发现了非常有趣的元素.绝对看看下面发布的链接,探索主题样式.

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml


Rag*_*dan 14

https://groups.google.com/forum/#!topic/android-developers/NDFo9pF8sHY

来自Dianne Hackborn的建议

使用非对话框主题作为android.R.style.Themeandroid.R.style.Theme_Light.

看@主题

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml.

检查此链接

http://developer.android.com/reference/android/app/DialogFragment.html

DialogFragment picker = MyDialogFragment.newInstance();
picker.setStyle( DialogFragment.STYLE_NORMAL, android.R.style.Theme );
picker.show(getFragmentManager(), "MyDialogFragment");
Run Code Online (Sandbox Code Playgroud)


Mar*_*nci 8

如果您将此主题设置为对话框,它将始终为全屏

<!-- DIALOG STYLE -->
<style name="You.Dialog" parent="android:Theme.Holo.Dialog" >
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)

为此,您可以使用此setStyle(int,int)方法.

dialogFragment.setStyle( DialogFragment.STYLE_NORMAL, R.style.You_Dialog );
Run Code Online (Sandbox Code Playgroud)


Aym*_*oub 7

首先你要知道Dialog fragment中全屏的处理和普通的Dialog组件是不同的,其次你需要在实际创建对话框之前自定义Dialog fragment@(OnCreateDialog),根据“user3244180”的回答完整屏幕对话框片段

 @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.TRANSPARENT));
     dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

     return dialog;
 }
Run Code Online (Sandbox Code Playgroud)


and*_*rew 6

这对我有帮助,支持<API 11.

创建一个全屏且具有透明背景的样式:

<style name="TransparentDialog" parent="@android:style/Theme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:background">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后你的DialogFragment代码:

public class MyDialog extends DialogFragment {

     public static MyDialog newInstance() {
         MyDialog dialog = new MyDialog();
         dialog.setStyle(DialogFragment.STYLE_NO_FRAME, R.style.TransparentDialog);
         return dialog;
     }

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_custom_layout, container, false);
        return view;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,为了清楚起见,my_custom_layout.xml的内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.kabx"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/semi_transparent_grey" >

..........................
..Whatever You Want Here..
..........................

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)


rui*_*dge 5

我之前遇到过这个问题:设置全屏模式时总是会有填充。在dialogFragment的onActivityCreated()方法中尝试以下代码:

public void onActivityCreated(Bundle savedInstanceState)
{   
    Window window = getDialog().getWindow();
    LayoutParams attributes = window.getAttributes();
    //must setBackgroundDrawable(TRANSPARENT) in onActivityCreated()
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    if (needFullScreen)
    {
        window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }
}
Run Code Online (Sandbox Code Playgroud)