DialogFragment fullscreen shows padding on sides

社会主义新天地 提交于 2019-11-27 19:02:13

I figured it out using custom dialog theme. windowIsFloating true will get rid of the background but will add some extra space underneath the background as a background. In which case you can use windowBackground @null to erase it.

<style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
    <item name="android:windowBackground">@null</item>
    <item name="android:windowIsFloating">true</item>
</style

Thank you to Raghunandan who gave me the link that includes all style attributes. It took me a while but I went through that file and found very interesting elements. Definitely have a look at the link posted below to explore theme styles.

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

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

From Dianne Hackborn suggestion

Use non-dialog theme as android.R.style.Theme or android.R.style.Theme_Light.

Look @ the themes

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

Check this link

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");

if you set this theme to your dialog it will always be fullscreen

<!-- 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>

to do so you can use this setStyle(int,int) method.

dialogFragment.setStyle( DialogFragment.STYLE_NORMAL, R.style.You_Dialog );

This worked for me with support for < API 11.

Create a style that is both full screen and has a transparent background:

<style name="TransparentDialog" parent="@android:style/Theme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:background">@android:color/transparent</item>
</style>

Then your DialogFragment code:

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;
    }
}

Finally, just for clarity, the contents of 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>

I met the issue before: there is always a padding while having set fullscreen. try this code in dialogFragment's onActivityCreated() method:

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);
    }
}
Ayman Mahgoub

First you need to know that handling of full screen in Dialog fragment is different from the normal Dialog component, second you need to customize the Dialog fragment before the actual creation of the dialog @ (OnCreateDialog), according to the answer of "user3244180" Full screen DialogFragment

 @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;
 }

DialogFragment (when not told explicitly), will use its inner styles that will wrap your custom layout in it (no fullscreen, etc.).

No need to create custom theme for this problem. Just use this method and set style and theme of your choosing:

/* theme is optional, I am using leanback... */
setStyle(STYLE_NORMAL, R.style.AppTheme_Leanback);

I was getting huge side padding in the DialogFragment for Nexus 6p and Pixel.

Fixed that by defining custom style as follows:

<style name="Custom.Dialog" parent="android:Theme.DeviceDefault.Dialog.NoActionBar.MinWidth" >
        <item name="android:windowBackground">@null</item>
        <item name="android:windowIsFloating">true</item>
</style>

parent="android:Theme.DeviceDefault.Dialog.NoActionBar.MinWidth did the trick

For fullscreen dialog I have posted a answer

In this thread

Please check this is the most efficient and shortest way. Hope this helps :)

<style name="WideDialog" parent="Theme.AppCompat.Light.DialogWhenLarge">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowIsFloating">false</item>
    </style>

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.WideDialog);
    }

The following works perfectly for me. It lets me have a full-width dialog (fills the screen's width with no padding) but with wrap_content for height, and it retains all my other stylings that I do in my builder:

<style name="DialogTheme">

    <item name="windowMinWidthMajor">100%</item>
    <item name="windowMinWidthMinor">100%</item>

    <item name="android:windowBackground">@null</item>
    <item name="android:windowIsFloating">true</item>

    <item name="android:background">#ffffff</item>

</style>

Background is required or else it does a weird repeat thing, but just set this to the color you want your dialog background to be. WindowBackground and WindowIsFloating are required to make the size wrap correctly.

Add your theme to your builder like so:

builder = new AlertDialog.Builder(_context, R.style.DialogTheme);

and you're good to go!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!