主题不适用于Android上的DialogFragment

LuT*_*ieR 43 compatibility android themes styles dialog

我正在将旧的Dialogs切换到DialogFragment,但主题和样式似乎不起作用.

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

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

谢谢!


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

小智 48

您不应该将AlertDialog.Builder(Context,int)构造函数与支持库一起使用,因为它仅在API 11之后可用.

要在对话框中设置主题,请使用ContextThemeWrapper,如下所示:

ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Dialog_NoActionBar);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
Run Code Online (Sandbox Code Playgroud)

  • 有没有人用自定义样式尝试过这个?当我传入预定义的android风格时,它可以工作,但如果我传入自己的风格则不行.(我的自定义样式是Theme.Dialog的子代) (5认同)

Blu*_*ell 29

我相信你需要在实际的Dialog而不是片段上设置主题

使用此构造函数创建AlertDialog:

 AlertDialog.Builder(Context context, int theme)
Run Code Online (Sandbox Code Playgroud)

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)
Run Code Online (Sandbox Code Playgroud)

  • 警告:如果你也在使用.setView(),你必须通过调用`getSystemService(Context.LAYOUT_INFLATER_SERVICE)`来获取同一个包装上下文中的LayoutInflater,以便在那里应用主题.浪费了2个小时才能找到它. (5认同)
  • 对话框在DialogFragment类上自动创建,如何获取对构建器的访问权限 (2认同)

pum*_*e65 18

我只是浪费了很多时间,但我终于找到了一种完全在xml中完成此操作的方法.

在应用程序主题中,Dialogs实际上是分开的主题.因此,要使用绿色按钮和绿色EditText提示设置所有DialogFragments的样式,您可以创建如下样式:

<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>
Run Code Online (Sandbox Code Playgroud)

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

<style name="MyTheme" parent="android:Theme.Holo.Light">
    <item name="android:dialogTheme">@style/DialogTheme</item>
</style>
Run Code Online (Sandbox Code Playgroud)

非常感谢谁写了这篇文章,告诉我通往我一直在寻找的道路!