2013-07-25 55 views
0

我在style.xml一些小的定製:的Android防止活性風格被應用於alertdialogs

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar"></style> 

    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <item name="android:background">@color/light_grey</item> 
     <item name="android:textColor">@color/white</item> 
    </style> 

現在的造型是正確應用到我的活動。

但是,當我創建一個alertdialog,背景顏色應用於對話框的標題和正文,我不想。我希望alertdialog保持其股票樣式。

這裏是alertdialog:

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      builder.setTitle("Wtitle").setMessage("message"); 
      builder.setNeutralButton("ok", null); 
      builder.show(); 

誰能幫助?

+0

您是否將AppBaseTheme應用到應用程序,而不是活動? – sandrstar

回答

0

有工作的解決方案 - 使用另一個AlertDialog.Builder構造函數主題爲the documentation描述,想法基本上是來自這個"How to change theme for AlertDialog"

  • 有沒有在你的styles.xml一個奇怪的事情:應用主題定義android:background代替android:windowBackground。似乎沒有理由這樣做,因爲如果你需要相同的背景所有意見(我懷疑是可能的),那麼你可以有一些觀點的基本主題。我認爲,干擾應用程序主題和視圖主題基本上不是一個好主意,因爲應用程序需要完全不同的屬性。所以,讓我們做了以下:

    <resources> 
        <style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar"></style> 
    
        <!-- Application theme. --> 
        <style name="AppTheme" parent="AppBaseTheme"> 
         <item name="android:windowBackground">@color/light_grey</item> 
         <item name="android:textColor">@color/white</item> 
        </style> 
    </resources> 
    
  • 對話框生成應該與它自己的主題創建:

    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Dialog)); 
    

    這裏請注意,由於某種原因,構造AlertDialog.Builder (Context context, int theme)不這樣做的權利和ContextThemeWrapper是必要的(看起來這是因爲不是所有的屬性都在主題中,主題只能使用ContextThemeWrapper重新創建)。