我有一個主要的活動,用戶可以在其中更改(通過首選項)背景顏色爲他們最喜歡的顏色。我的問題是我無法更改任何自定義對話框的背景顏色。如何以編程方式更改對話框背景顏色?
在堆棧溢出其他答案提示:
的(a)overiding the default theme的優選顏色。我不認爲這是一種合適的解決方案,因爲我知道不推薦在運行時更改主題。
(B)Changing in styles.xml(不適合在這種情況下,我不能在運行時更改)
(C)Overriding the AlertBuilder class(但這種色調整個警告對話框)
這最接近我來了要更改顏色,請移除警報生成器標題,並將自定義視圖的背景設置爲最喜歡的顏色(eg.pink)。不幸的是,這會在對話框的頂部和底部給出一個醜陋的條。
代碼包含在圖像後,有關如何更改對話框背景的建議將不勝感激。
代碼的默認外觀
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);
}
你能完成這個對話框嗎?如果是的話,你可以分享嗎? – pengwang