How to change the background color around a DialogFragment?

前端 未结 8 1802
时光取名叫无心
时光取名叫无心 2020-11-29 01:59

I\'m building a custom DialogFragment. The dialog layout is set to my_dialog.xml, but how can I modify the color around the dialog (the transparent

相关标签:
8条回答
  • 2020-11-29 02:16

    Those who are using AlertDialog builder in 'onCreateDialog' instead of 'onCreateView' can assign theme like following code. Complete set of themes can be found from R.style. Don't forget that some of them supported recently and are not available on old device phones.

    @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Translucent);
            View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_album, null);
            builder.setView(view);
    
            return builder.create();
        }
    
    0 讨论(0)
  • 2020-11-29 02:20

    I found I just needed to do this:

    <style name="MyDialog" parent="@android:style/Theme.Dialog">
        <!--  other attributes  -->
        <item name="android:backgroundDimEnabled">false</item>
    </style>
    
    0 讨论(0)
  • 2020-11-29 02:22

    I wanted a transparent background for my DialogFragment, and, in code, this works fine:

    @Override
    public void onStart() {
        super.onStart();
    
        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }
    

    Of course, you can specify any color or Drawable using setBackgroundDrawable() or setBackgroundDrawableResource().

    This works at least in onStart(), but not in onCreate(), and not necessarily in onCreateView(), it seems.

    That said, in most cases it's probably cleaner to do this in XML, using styles, along these lines:

    <style name="MyDialogStyle" parent="@android:style/Theme.Holo.Light.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>
    
    0 讨论(0)
  • 2020-11-29 02:24

    I had to set android:windowIsFloating to false and android:windowBackground to my custom color in the dialog style:

    styles.xml

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
    
        <style name="MyDialog" parent="@android:style/Theme.Dialog">
            <item name="android:windowFrame">@null</item>
            <item name="android:windowBackground">@color/orange_transparent</item>
            <item name="android:windowIsFloating">false</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowTitleStyle">@null</item>
            <item name="android:colorBackgroundCacheHint">@null</item>
            <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
            <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
            <item name="android:gravity">center</item>
        </style>
    
    </resources>
    

    MyDialogFragment

    public class MyDialogFragment extends DialogFragment {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
        }
    }
    
    0 讨论(0)
  • 2020-11-29 02:26

    Overriding onCreate and setting the style there should work.

    @Override
    public void onCreate(Bundle savedInstance){
        super.onCreate(savedInstance);
        setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent);
    }
    
    0 讨论(0)
  • 2020-11-29 02:27

    Jul's answer was almost good for me. But it seems it doesn't work when using an AlertDialog.Builder.

    I had to set the style here:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialog );
    
    0 讨论(0)
提交回复
热议问题