Theme not applying to DialogFragment on Android

流过昼夜 提交于 2019-11-30 11:44:31

问题


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 v4, and in the onCreate method I've tried calling setStyle(style, theme); with a lot of different themes, but the dialog always shows as an "old" dialog in the emulator running Android 4.0.3 (i.e., it does not shows in Holo theme).

Is there anything else that I should be doing? Does using the compatibility library disables the Holo theme or anything? If this is the case, should I create two DialogFragments, one for older versions and one for newer versions?

Thanks!


Here's the (simplified) code for my dialog. I've tried both Theme_Holo_Dialog_NoActionBar and Theme_DeviceDefault_Dialog_NoActionBar, but the Android 4 emulator always shows the dialog as an "old" dialog instead of using the Holo theme. What am I doing wrong? :(

[...]
import android.support.v4.app.DialogFragment;
[...]

public class AlertDialogFragment extends DialogFragment {

  public static AlertDialogFragment newInstance(int id) {

    AlertDialogFragment f = new AlertDialogFragment();
    Bundle args = new Bundle();
    args.putInt("id", id);
    f.setArguments(args);

 }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int style = DialogFragment.STYLE_NORMAL, theme = 0;
    theme = android.R.style.Theme_Holo_Dialog_NoActionBar;
    setStyle(style, theme);     
  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {

    mId = getArguments().getInt("id");
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
        .setTitle(mTitle)
        .setMessage(mMessage)
        .setPositiveButton(getString(R.string.btn_ok), new DialogInterface.OnClickListener() {      
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dismiss();                  
            }
        });
        return builder.create();
    }

回答1:


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)



回答2:


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



回答3:


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!




回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/10591221/theme-not-applying-to-dialogfragment-on-android

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