2016-12-26 49 views
2

我想設置AlertDialogue主題或更改背景顏色。警報對話框背景主題/顏色

雖然我知道它有一個默認的主題,但在不同的版本我得到不同的主題,所以我想修復它的所有版本。

或者簡單地改變背景顏色爲白色

@NonNull 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     final SimpleAdapter adapter = new SimpleAdapter(getContext(), imagesWithNames, R.layout.lib_dialog_image, 
       new String[]{"name", "imageID","Spacing"}, new int[]{R.id.text1, R.id.image1,R.id.spacing}); 
     return new AlertDialog.Builder(getContext()).setAdapter(adapter, 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         ((PlaceCallActivity) getContext()).OnSelected(WithNamesFragment.this.getClass(), (int) ((HashMap<String, Object>) adapter.getItem(i)).get("imageID")); 
        } 
       }).setCancelable(true).setTitle("PICK YOUR AVATAR").setNegativeButton("Cancel",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }).create(); 
    } 

不要發佈您的代碼,請告訴我應該在哪裏做出改變這裏。

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

注:上述行會做,但我想知道我應該給樣式我AlertDialogue

enter image description here

+0

你還在尋找解決方案嗎? –

+0

是的,我還沒有找到一個! – SamH67

+0

參考這一個http://stackoverflow.com/questions/2422562/how-to-change-theme-for-alertdialog –

回答

5

styles.xml文件中創建自己的風格,如下所示。

<style name="AlertDialogCustom" parent="@android:style/Theme.Dialog"> 
     <item name="android:textColor">@color/White</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:headerDividersEnabled">true</item> 
     <item name="android:typeface">normal</item> 
     <item name="android:background">@color/colorPrimaryDark</item> 
    </style> 

然後創建Alert Dialog使用生成器如下

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(NewCase.this, R.style.AlertDialogCustom)); 

這裏路過當前類語境和風格的ContextThemeWrapper類的構造函數。

+0

首先看看,告訴我我應該做什麼,我不能把這個添加到我的返回對話框中,不能將Ankit Shah做正確的,但有一些問題 – SamH67

+0

@ SamH67告訴我到底你想要什麼。然後我可以幫你。 –

+0

我想這樣做,因爲我無法添加您的代碼。看看我的代碼,並告訴我如何添加,但請在嘗試之前嘗試,因爲在發佈此問題之前我已經嘗試了這個東西,所以我已經知道這個東西,但是我能夠在該return語句中更改主題 – SamH67

2

您應該添加對話框樣式裏面RES /價值/風格.XML。如下所示。

<style name="MyDialog" parent="@android:style/Theme.Holo.Light.Dialog"> 
     <item name="android:background">@color/white</item> 
    </style> 

或者你改變背景顏色也如下:

編輯:

getWindow().setBackgroundDrawableResource(R.color.white); 
+0

getWindow無法解析 – SamH67

+0

@ SamH67我的答案已更新,請檢查它。 –

相關問題