2015-11-23 21 views
1

我有一個AlertDialog,用戶必須在幾個選項之間進行選擇。黑暗背景上的默認顏色方案是白色文字。在應用程序中,用戶可以配置他們自己的顏色,以便與白色相匹配。將背景更改爲白色不是問題,但我無法設置項目的文本顏色。Android - 使用列表以編程方式在AlertDialog上設置TextColor ListItem

我在找dialog.getItems.setTextColor([usercolor]);之類的東西,但是我還沒有找到解決方案。在構建對話框的代碼下面。

tvRekening.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       AlertDialog.Builder builder = new AlertDialog.Builder(a); 
       builder.setTitle("Title") 
         .setItems(values, new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, int which) { 
           // The 'which' argument contains the index position 
           // of the selected item 
           setRekeningView(which); 
          } 
         }); 
       AlertDialog dialog = builder.create(); 
       dialog.getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(a, R.color.white))); 
       dialog.show(); 
      } 
     }); 

爲了進一步說明這個問題:

默認顏色方案: http://i.stack.imgur.com/nMKAA.png

改變背景顏色爲白色,使文本不可讀的(因爲它仍然是白色的)。 http://i.stack.imgur.com/PEABc.png

編輯: 我知道我可以爲此創建一個自定義適配器。但創建一個適配器只用於編輯項目的文本顏色似乎有點矯枉過正。如果有人知道解決方案而不會遇到創建適配器的麻煩,那將是不勝感激。如果沒有,我會在一段時間後關閉這個問題。

+0

爲了改變文本的顏色需要創建佈局定製適配器,然後將它傳遞給setAdapter方法 –

回答

1

您需要創建自定義對話框是這樣的:

Dialog d =new Dialog(context); 
d.setContentView(R.layout.dialog); 
ListView l=((ListView)d.findViewById(R.id.list)); 

看到此鏈接Android - Executing a custom listview in a custom dialog properly

+0

創建適配器在我看來這是過度的。我正在尋找更簡單的解決方案。 –

+0

雖然我很失望,但我會把這個標記爲正確的,因爲看起來沒有別的辦法。 –

相關問題