DialogFragment fullscreen shows padding on sides

后端 未结 11 1588
囚心锁ツ
囚心锁ツ 2020-12-05 00:22

I am creating a custom DialogFragment that is displayed underneath the actionbar. So far everything works great. The layout parameters for dialog fragment are match_pa

相关标签:
11条回答
  • 2020-12-05 00:36

    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 );
    
    0 讨论(0)
  • 2020-12-05 00:36

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 00:37

    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;
     }
    
    0 讨论(0)
  • 2020-12-05 00:38

    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>
    
    0 讨论(0)
  • 2020-12-05 00:40

    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!

    0 讨论(0)
  • 2020-12-05 00:43

    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");
    
    0 讨论(0)
提交回复
热议问题