2011-09-01 158 views
18

我有一個主要的活動,用戶可以在其中更改(通過首選項)背景顏色爲他們最喜歡的顏色。我的問題是我無法更改任何自定義對話框的背景顏色。如何以編程方式更改對話框背景顏色?

在堆棧溢出其他答案提示:

的(a)overiding the default theme的優選顏色。我不認爲這是一種合適的解決方案,因爲我知道不推薦在運行時更改主題。

(B)Changing in styles.xml(不適合在這種情況下,我不能在運行時更改)

(C)Overriding the AlertBuilder class(但這種色調整個警告對話框)

這最接近我來了要更改顏色,請移除警報生成器標題,並將自定義視圖的背景設置爲最喜歡的顏色(eg.pink)。不幸的是,這會在對話框的頂部和底部給出一個醜陋的條。

代碼包含在圖像後,有關如何更改對話框背景的建議將不勝感激。

Dialog appearance

代碼的默認外觀

protected Dialog onCreateDialog(int dialogId) { 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 


     final View viewMessEdit = inflater.inflate(R.layout.example,(ViewGroup) findViewById(R.id.dialog_mess_edit_root)); 
       builder.setView(viewMessEdit); 
     builder.setTitle("Alert builder's title"); 
} 

代碼改變用戶對話視圖背景色(和警報建設者的標題被刪除)

protected Dialog onCreateDialog(int dialogId) { 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 


     final View viewMessEdit = inflater.inflate(R.layout.example,(ViewGroup) findViewById(R.id.dialog_mess_edit_root)); 
       builder.setView(viewMessEdit); 
       viewMessEdit.setBackgroundResource(R.color.pink_dark); 

} 
+0

你能完成這個對話框嗎?如果是的話,你可以分享嗎? – pengwang

回答

8

我有點面臨同樣的問題。解決這個問題的唯一方法就是擴展我自己的佈局版本。我看你的情況是AlertDialog。我建議你做的是創建一個獨特的類,就是你自定義的AlertDialog併爲此創建一個佈局,然後你膨脹這個。

這是一個幫助我很多的帖子。 http://blog.androgames.net/10/custom-android-dialog/

我跟着這篇文章,並解決了我的問題與自定義對話框。

請,如果你有更多的疑問,請告訴我。

謝謝。

+1

Thankyou鏈接到androgames帖子。它寫得很清楚,評論很好。這裏是相關的博客文章。 http://dtmilano.blogspot.com/2010/01/android-ui-colored-dialogs.html – Mel

+15

@rogcg鏈接不工作。 – Deepak

+0

我已經使用WayBack機器更新了緩存版本的鏈接。 – kabuko

17

我發現了一個規則解決方案!

d.getWindow().setBackgroundDrawableResource(R.drawable.menubackground); 

它適用於我的正常對話框。 但我不知道它是否適用於AlertDialog

+2

我剛試過,它沒有。我嘗試了一種純色,而我的顏色出現在後臺,而不是alertdialog的陰影。 – soger

+0

@ user3696814它在AlertDialog中工作。 – CopsOnRoad

3

我發現了一個黑客,沒有創建自定義佈局,您可以通過玩AlertDialog的某些屬性來創建多個設計。

你要做的:

AlertDialog是您的屏幕上可見,OnShowListener被調用。因此,通過添加dialog.setOnShowListener(this),您將能夠自定義您的AlertDialog

代碼:

// Create AlertDialog 
AlertDialog.Builder adb = new AlertDialog.Builder(context1); 
    adb.setTitle(context1.getString(R.string.app_name)) 
    .setMessage(message) 
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

     } 
}); 
AlertDialog dialog = adb.create(); 

// Make some UI changes for AlertDialog 
dialog.setOnShowListener(new DialogInterface.OnShowListener() { 
    @Override 
    public void onShow(final DialogInterface dialog) { 

     // Add or create your own background drawable for AlertDialog window 
     Window view = ((AlertDialog)dialog).getWindow(); 
     view.setBackgroundDrawableResource(R.drawable.your_drawable); 

     // Customize POSITIVE, NEGATIVE and NEUTRAL buttons. 
     Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE); 
     positiveButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); 
     positiveButton.setTypeface(Typeface.DEFAULT_BOLD); 
     positiveButton.invalidate(); 

     Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE); 
     negativeButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); 
     negativeButton.setTypeface(Typeface.DEFAULT_BOLD); 
     negativeButton.invalidate(); 

     Button neutralButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEUTRAL); 
     neutralButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); 
     neutralButton.setTypeface(Typeface.DEFAULT_BOLD); 
     neutralButton.invalidate(); 
    } 
}); 
+0

就像一個魅力)thx –

1

建議的解決方案

d.getWindow().setBackgroundDrawableResource(R.drawable.menubackground); 

不要工作得很好(在6.0測試),因爲它改變了對話陰影

這裏是我的解決方案:

public static void changeDialogBackground(final Dialog dialog,final int resId) { 
    dialog.getWindow().getDecorView().setBackgroundResource(resId); 
    dialog.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(final DialogInterface dialog) { 
      Window window = ((AlertDialog)dialog).getWindow(); 
      window.getDecorView().setBackgroundResource(resId); 
     } 
    }); 
} 
3

對話背景顏色

dialog.getWindow().setBackgroundDrawableResource(R.color.glassblack); 

果然奏效對我很好。

相關問題