2015-05-29 152 views
4

我最近在我的應用程序中將AlertDialog實例切換爲使用android.support.v7.app.AlertDialog,但在將自定義主題和樣式應用於警報對話框。使用android.support.v7.app.AlertDialog自定義默認Theme.AppCompat.Dialog.Alert

在我styles.xml我有以下樣式:

<style name="AlertDialog" parent="Theme.AppCompat.Dialog.Alert"> 
    <item name="colorAccent">@color/theme_accent_1</item> 
    <item name="background">@color/theme_component_background</item> 
    <item name="android:background">@color/theme_component_background</item> 
</style> 

在我的themes.xml我有以下我的主題,根據需要設置:

<item name="android:alertDialogTheme">@style/AlertDialog</item> 

對於某些原因,自定義樣式永遠不會應用於AlertDialog實例,並且它們仍然是默認的Theme.AppCompat.Dialog.Alert主題。

如果我明確地設置樣式資源使用如下,它的工作:

AlertDialog.Builder builder = 
    new AlertDialog.Builder(getActivity(), R.style.AlertDialog); 

有沒有辦法設置默認的風格在整個主題,而不必在Builder指定主題構造函數?

+2

閱讀它http://stackoverflow.com/questions/18307622/applying-a-theme-to-v7-support-action-bar。它對你有幫助嗎? –

回答

7

根據Pankaj Kumar的迴應,鏈接詳細說明了解決方案,即在name屬性中同時包含和不包含「android:」前綴的聲明。

<item name="android:alertDialogTheme">@style/AlertDialog</item> 
<item name="alertDialogTheme">@style/AlertDialog</item>