我将旧的Dialogs切换为DialogFragment,但是主题和样式似乎不起作用。

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

还有什么我应该做的吗?使用兼容性库是否会禁用Holo主题或其他功能?如果是这样,我是否应该创建两个DialogFragment,一个用于较旧版本,一个用于较新版本?

谢谢!

这是我的对话框的(简化)代码。我已经尝试了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();
    }

最佳答案

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

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

 AlertDialog.Builder(Context context, int theme)

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

10-05 20:23
查看更多