2012-02-25 37 views
2

所以我一直在尋找大約一個半小時,但我無法弄清楚。我不想設置自定義主題。我想爲AlertDialog設置一個內置於android的主題。如何在Android的AlertDialog中設置主題

根據d.android.com我可以這樣做:

public AlertDialog.Builder (Context context) 

或本

public AlertDialog.Builder (Context context, int theme) 

所以我做到這一點,它完美的作品:

AlertDialog action_btn = new AlertDialog.Builder(MyActivity.this).create(); 

但是,當我需要添加一個主題時,我從eclipse得到一個錯誤:

AlertDialog action_btn = new AlertDialog.Builder(MyActivity.this, AlertDialog.THEME_TRADITIONAL).create(); 

如果有人能夠幫助我設置一個主題,那麼我對編程仍然很陌生。

我也有獎金的問題:

我不能得到公正AlertDialog()工作,使其工作,我需要鍵入AlertDialog.Builder(),但開發商的網站上,他們似乎都具有相同的方法和構造。有什麼區別/爲什麼AlertDialog()只是工作?

回答

4

AlertDialog.Builder的主題僅適用於Android 3.0及更高版本(API級別11)。看起來您已經在您的項目設置中設置了較早的Android版本。

Android引用顯示所有構造函數和方法的API級別。你甚至可以設置一個過濾器,只顯示你的API級別可用的方法。

Read more about API levels here.

+0

唉可用。我無法相信。我看到它說「自API級別以來!」所以我認爲一切都是有效的,但是如果你向下滾動大約1/3的頁面,它就會說它只在「API Level 11」中有效。謝謝。這種簡單的解決方案。 – EGHDK 2012-02-26 01:09:48

13

裹主題爲背景,這是因爲API級別1

Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(
    this,android.R.style.Theme_DeviceDefault_Light_Dialog)); 
+2

如果您在使用API​​低於API 11的API時尋找對話框樣式的解決方案,則此處爲 – 2014-06-11 07:49:54