主题不适用于DialogFragment在Android不适用于、主题、Android、DialogFragment

2023-09-05 00:15:34 作者:荒凉一梦

我交换我的老对话框来DialogFragment,但主题和风格似乎并不奏效。

我使用的是从兼容性库V4的DialogFragment,并在onCreate方法我已经打过电话的setStyle(风格,主题);有很多不同的主题,但总的对话框显示为运行Android 4.0.3的老字号在仿真器对话框(例如,它没有在全息主题表演)。

还有什么是我应该做的?是否使用兼容性库禁用的河洛主题或什么?如果是这样的话,我应该创建两个DialogFragments,一个是旧版本,一个是更新的版本?

谢谢!

下面是(简化)code我的对话。我都试过Theme_Holo_Dialog_NoActionBar和Theme_DeviceDefault_Dialog_NoActionBar,但安卓4.0模拟器始终显示在对话框作为一个老对话框,而不是使用的Holo主题。我究竟做错了什么? :(

  [...]
进口android.support.v4.app.DialogFragment;
[...]

公共类AlertDialogFragment扩展DialogFragment {

  公共静态AlertDialogFragment的newInstance(INT ID){

    AlertDialogFragment F =新AlertDialogFragment();
    捆绑的args =新包();
    args.putInt(身份证,身份证);
    f.setArguments(参数);

 }

  @覆盖
  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    INT风格= DialogFragment.STYLE_NORMAL,主题= 0;
    主题= android.R.style.Theme_Holo_Dialog_NoActionBar;
    的setStyle(风格,主题);
  }

  @覆盖
  公共对话onCreateDialog(包savedInstanceState){

    MID = getArguments()调用getInt(ID)。
    AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity())
        .setTitle(mTitle)
        .setMessage(mMessage)
        .setPositiveButton(的getString(R.string.btn_ok),新DialogInterface.OnClickListener(){
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                解雇();
            }
        });
        返回builder.create();
    }
 

解决方案

我相信你需要设置的实际对话主题,而不是片段

技术问答 OSCHINA

使用此构造函数来创建AlertDialog:

  AlertDialog.Builder(上下文的背景下,诠释主题)
 

  AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity(),主题)
 

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

解决方案

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)