Theme not applying to DialogFragment on Android

后端 未结 5 1831
孤独总比滥情好
孤独总比滥情好 2020-12-13 23:59

I\'m switching my old Dialogs to DialogFragment, but the themes and styles don\'t seem to be working.

I\'m using the DialogFragment from the compatibility library v

相关标签:
5条回答
  • 2020-12-14 00:10

    You shoudn't use the AlertDialog.Builder(Context, int) constructor with the support library because it is only available since API 11.

    To setup a theme to your dialog, use instead a ContextThemeWrapper like this:

    ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Dialog_NoActionBar);
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    
    0 讨论(0)
  • 2020-12-14 00:10

    Here is a more current answer, using target SDK of 23 and min SDK of 14, this code works perfectly for me.

    The main change from the code in the question is setting the theme in the constructor, only overriding onCreateDialog(), and using the AlertDialog class from the v7 support library.

    Using this code, the green text flat (borderless) buttons are shown on 4.4.4 as opposed to the default gray buttons with borders.

    import android.support.v7.app.AlertDialog;
    import android.app.Dialog;
    import android.support.v4.app.DialogFragment;
    import android.content.DialogInterface;
    import android.os.Bundle;
    
    public class MyDialog extends DialogFragment {
    
        String message;
    
        public MyDialog(String m) {
            message = m;
            int style = DialogFragment.STYLE_NORMAL, theme = 0;
            theme = android.R.style.Theme_Holo_Dialog_NoActionBar;
            setStyle(style, theme);
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            return new AlertDialog.Builder(getActivity())
                    .setMessage(message)
                    .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((EnterPhoneNumberActivity)getActivity()).doPositiveClick();
                            }
                        }
                    )
                    .setNegativeButton("EDIT",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((EnterPhoneNumberActivity)getActivity()).doNegativeClick();
                            }
                        }
                    )
                    .create();
        }
    }
    

    Usage in an AppCompatActivity:

        String message = "test";
        if (message != null) {
            DialogFragment newFragment = new MyDialog(message);
            newFragment.show(getSupportFragmentManager(), "dialog");
        }
    
    0 讨论(0)
  • 2020-12-14 00:12

    I believe you need to set the theme on the actual Dialog and not the Fragment

    Use this constructor to create your AlertDialog:

     AlertDialog.Builder(Context context, int theme)
    

    ie

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)
    
    0 讨论(0)
  • 2020-12-14 00:19

    I just lost a lot of time to this, but I finally found a way to do this entirely in xml.

    In an application theme, Dialogs are actually themed separately. So to style all DialogFragments with green buttons and green EditText hints, you would make a style like this:

    <style name="DialogTheme" parent="@android:style/Theme.Holo.Light.Dialog">
        <item name="android:buttonStyle">@style/button_green</item>
        <item name="android:textColorHint">@color/green</item>
    </style>
    

    Then add this theme to your application theme as the dialogTheme

    <style name="MyTheme" parent="android:Theme.Holo.Light">
        <item name="android:dialogTheme">@style/DialogTheme</item>
    </style>
    

    Many thanks to whoever wrote this post for showing me the path to what I'd been searching for!

    0 讨论(0)
  • 2020-12-14 00:23

    You should write these codes in "onCreateView".

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_your_theme, container);
        return view;
    }
    
    0 讨论(0)
提交回复
热议问题