2014-10-29 43 views
1

我需要一些指南來解決Android主題的問題。我無法更改我的AlertDialog主題

我已經創建了一個自定義主題,以便將其應用於警報對話框。一切似乎都很好,但是,當我嘗試使用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 /價值/ 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> 

謝謝!

回答

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

您訪問是你自己,而不是機器人的資源,嘗試上述解決方案。

+0

啊,現在我明白爲什麼它失敗了。 – nanonymous 2014-10-29 16:52:36

1

除非我丟失東西,android.R.style.MyTheme應該是R.style.MyTheme。目前,您正在嘗試從Android的資源中引用MyTheme的樣式,而不是您的資源。