主题不适用于 Android 上的 DialogFragment

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10591221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:15:52  来源:igfitidea点击:

Theme not applying to DialogFragment on Android

androiddialogstylesthemescompatibility

提问by LuTHieR

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

我正在将旧对话框切换到 DialogFragment,但主题和样式似乎不起作用。

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

我正在使用兼容性库 v4 中的 DialogFragment,并且在 onCreate 方法中我尝试调用 setStyle(style, theme); 有很多不同的主题,但对话框总是在运行 Android 4.0.3 的模拟器中显示为“旧”对话框(即,它不会在 Holo 主题中显示)。

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?

还有什么我应该做的吗?使用兼容性库是否会禁用 Holo 主题或其他任何东西?如果是这种情况,我是否应该创建两个 DialogFragment,一个用于旧版本,一个用于新版本?

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? :(

这是我的对话框的(简化)代码。我已经尝试了 Theme_Holo_Dialog_NoActionBar 和 Theme_DeviceDefault_Dialog_NoActionBar,但 Android 4 模拟器总是将对话框显示为“旧”对话框,而不是使用 Holo 主题。我究竟做错了什么?:(

[...]
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();
    }

采纳答案by Blundell

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

我相信您需要在实际的 Dialog 而不是 Fragment 上设置主题

Use this constructor to create your AlertDialog:

使用此构造函数创建您的 AlertDialog:

 AlertDialog.Builder(Context context, int theme)

ie

IE

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)

回答by Florian

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

您不应将 AlertDialog.Builder(Context, int) 构造函数与支持库一起使用,因为它仅从 API 11 开始可用。

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

要为您的对话框设置主题,请改为使用 ContextThemeWrapper,如下所示:

ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Dialog_NoActionBar);
AlertDialog.Builder builder = new AlertDialog.Builder(context);

回答by pumpkinpie65

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

我只是为此浪费了很多时间,但我终于找到了一种完全在 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:

在应用程序主题中,对话框实际上是单独的主题。因此,要使用绿色按钮和绿色 EditText 提示设置所有 DialogFragment 的样式,您可以创建如下样式:

<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

然后将此主题添加到您的应用程序主题中作为 dialogTheme

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

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

非常感谢写这篇文章的人向我展示了我一直在寻找的路径!

回答by Daniel Nugent

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

这是一个更新的答案,使用 23 的目标 SDK 和 14 的最小 SDK,此代码非常适合我。

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.

问题中代码的主要变化是在构造函数中设置主题,仅覆盖onCreateDialog(),并使用 v7 支持库中的 AlertDialog 类。

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

使用此代码,绿色文本平面(无边框)按钮显示在 4.4.4 上,而不是带有边框的默认灰色按钮。

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:

在 AppCompatActivity 中的用法:

    String message = "test";
    if (message != null) {
        DialogFragment newFragment = new MyDialog(message);
        newFragment.show(getSupportFragmentManager(), "dialog");
    }

回答by user3121777

You should write these codes in "onCreateView".

您应该在“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;
}