我无法改变我的AlertDialog主题

时间:2014-10-29 16:40:14

标签: android dialog themes

我需要一些指南来解决android主题的问题。

我创建了一个自定义主题,以便将其应用于警报对话框。但是,当我尝试使用ContextThemeWrapper将它应用到实际的对话框时,一切看起来都很好,但是当我无法识别android.R.style.MyTheme时。

这是我在MainActivity.java中的代码:

// Build CheckBox Dialog
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder checkboxDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), 
                android.R.style.MyTheme));

        checkboxDialogBuilder
                .setTitle("Pick the colors")
                .setSingleChoiceItems(items, -1,
                        new DialogInterface.OnClickListener() {

                            // indexSelected contains the index of item (of which checkbox checked)
                            @Override
                            public void onClick(DialogInterface dialog, int item) {
                                 switch(item)
                                    {
                                        case 0:
                                                // RGB choice
                                                RGB = true;
                                                break;

                                        case 1:
                                                // CMY choice
                                                RGB = false;
                                                break;
                                    }
                            }
                        })

                // Set the action buttons
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialog, int id) {
                                 // user clicked on OK
                                 setColorModeBackground();  
                             }
                })

                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialog, int id) {
                                // user clicked on Cancel
                                mDialog.dismiss();
                             }
                });

        AlertDialog customCheckboxDialog = checkboxDialogBuilder.create();

        customCheckboxDialog.show();

        return customCheckboxDialog;
    }

在这一行中我得到一个错误,因为android.R.style.Mytheme显然不存在,即使我在R.java文件中看到它。

AlertDialog.Builder checkboxDialogBu​​ilder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(),android.R.style.MyTheme));

R.java文件:

public static final class style {
    /** 
 Base application theme, dependent on API level. This theme is replaced
 by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


     Theme customizations available in newer API levels can go in
     res/values-vXX/styles.xml, while customizations related to
     backward-compatibility can go here.


    Base application theme for API 11+. This theme completely replaces
    AppBaseTheme from res/values/styles.xml on API 11+ devices.

    API 11 theme customizations can go here. 
     */

    public static final int AppBaseTheme=0x7f060000;

    /**  Application theme. All customizations that are NOT specific to a 
    particular API-level can go here. 
     */

    public static final int AppTheme=0x7f060001;
    public static final int MyTheme=0x7f060007;
    /** 
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.

     */
    public static final int base=0x7f060002;
    public static final int body=0x7f060004;
    public static final int customDialogTheme=0x7f060005;
    public static final int dialogTheme=0x7f060006;
    public static final int title=0x7f060003;
}

这是res / values / styles.xml中的文件:

    <style name="AppBaseTheme" parent="android:Theme.Light">
     <!--
         Theme customizations available in newer API levels can go in
         res/values-vXX/styles.xml, while customizations related to
         backward-compatibility can go here.
     -->
   </style>

   <!-- Application theme. -->
    <style name="MyTheme" parent="AppBaseTheme">
        <item name="android:alertDialogStyle">@style/customDialogTheme</item>
    </style>

    <style name="customDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:alertDialogStyle">@style/customDialogTheme</item>
        <item name="android:fullDark">@drawable/dialog_body</item>
        <item name="android:topDark">@drawable/dialog_title</item>
        <item name="android:centerDark">@drawable/dialog_body</item>
        <item name="android:bottomDark">@drawable/dialog_footer</item>
        <item name="android:fullBright">@drawable/dialog_body</item>
        <item name="android:centerBright">@drawable/dialog_body</item>
        <item name="android:bottomBright">@drawable/dialog_footer</item>
        <item name="android:bottomMedium">@drawable/dialog_footer</item>
        <item name="android:centerMedium">@drawable/dialog_body</item>
    </style>

提前致谢!

2 个答案:

答案 0 :(得分:1)

AlertDialog.Builder checkboxDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.MyTheme));

您正在访问的资源是您自己的,而不是机器人,请尝试上述解决方案。

答案 1 :(得分:1)

除非我遗漏了某些内容,android.R.style.MyTheme应为R.style.MyTheme。目前,您尝试从Android的资源中引用样式MyTheme,而不是您的。