2015-10-01 84 views
1

我無法弄清楚如何改變選擇首選項時彈出的警告對話框的分隔線顏色。我知道這是可能通過實施延伸AlertDialog和做一個自定義類的呈現以下()方法:如何更改PreferenceFragment警報對話框的分隔線顏色

int dividerId = getContext().getResources().getIdentifier("android:id/titleDivider", null, null); 
View divider = dialog.findViewById(dividerId); 
divider.setBackgroundColor(themeColor); 

然而,不知道如何強制出現在PreferenceFragment的警告對話框擴展我的自定義AlertDialog。

我也可以使用樣式來修改PreferenceFragment中AlertDialog的外觀,但沒有與AlertDialogs的分隔線顏色相對應的樣式屬性(因此必須實現查找分隔符視圖的黑客技巧)。

有沒有人知道如何實現這個可以實現我自己的PreferenceFragment?

我的應用程序的基本主題是Theme.AppCompat。

回答

2

這是所有關於AlertDialog改變顏色:

 AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Test Title"); 
     builder.setMessage(Html.fromHtml("<font color='#FF7F27'>This is a test</font>")); 
     builder.setCancelable(false); 
     builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
      } 
     }); 
     builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
      } 
     }); 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 
     try { 
      Resources resources = dialog.getContext().getResources(); 
      int alertTitleId = resources.getIdentifier("alertTitle", "id", "android"); 
      TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId); 
      alertTitle.setTextColor(Color.MAGENTA); // change title text color 

      int titleDividerId = resources.getIdentifier("titleDivider", "id", "android"); 
      View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId); 
      titleDivider.setBackgroundColor(Color.YELLOW); // change divider color 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     Button nbutton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE); 
     //Set negative button background 
     nbutton.setBackgroundColor(Color.MAGENTA); 
     //Set negative button text color 
     nbutton.setTextColor(Color.YELLOW); 
     Button pbutton = dialog.getButton(DialogInterface.BUTTON_POSITIVE); 
     //Set positive button background 
     pbutton.setBackgroundColor(Color.YELLOW); 
     //Set positive button text color 
     pbutton.setTextColor(Color.MAGENTA); 

這是我的示例代碼,但如果你想改變分頻器顏色考慮代碼的部分與「INT titleDividerId」開始。我希望它有幫助。

結果:

This is the result of the code

相關問題