2015-10-01 26 views
2

我有這樣 enter image description here如何定製的android AlertDialog文本屬性

我使用默認警報對話框警報對話框。我需要設置大小和顏色TitleContent和按鈕。現在,我使用下面的代碼

AlertDialog.Builder alertDialogBuilder =new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom)); 

而且我已經創建了一個風格資源,這也

<style name="AlertDialogCustom" parent="@android:style/Theme.Dialog"> 
    <item name="android:textColor">@color/medium_black</item> 
    <item name="android:textSize">20sp</item> 
    </style> 

我可以改變的Title的顏色和大小與

<item name="android:textColor">@color/medium_black</item> 
<item name="android:textSize">20sp</item> 

但它也正在申請是/否按鈕。我需要單獨的字體顏色和大小的所有這些3.可能嗎?

+0

你可以使用'alert.setView(R.layout.my_alert_view);' –

+0

可以通過默認對話框中的樣式來設置它。 – Drd

回答

1

如果你不想創建自定義對話框:

創建警報對話框後,只是顯示它之前,你可以這樣做:

final AlertDialog alertDialog = builder.create(); 
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 
      Button yesButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
      Button noButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE); 

      yesButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); 
      yesButton.setTypeface(/* "your type face" */); 

     } 
    }); 
    alertDialog.show(); 
0

使用html標籤樣式您的文本。

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 

      // set title 
      alertDialogBuilder.setTitle(Html.fromHtml("<font size='3' color='#FF7F27'>Set IP Address</font>")); 

      // set dialog message 
      alertDialogBuilder 
        .setMessage(Html.fromHtml("<font size='3' color='red'>Click yes to exit!</font>")) 
        .setCancelable(false) 
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog,int id) { 
          // if this button is clicked, close 
          // current activity 

         } 
        }) 
        .setNegativeButton("No", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          // if this button is clicked, just close 
          // the dialog box and do nothing 
          dialog.cancel(); 
         } 
        }); 

      // create alert dialog 
      AlertDialog alertDialog = alertDialogBuilder.create(); 

      // show it 
      alertDialog.show(); 
1

只需添加colorAccent屬性。它被應用於按鈕。

爲未來的探險S'more屬性:
可以使這樣的默認樣式...

<style name="CustomDialogAlert" parent="Theme.AppCompat.Light.Dialog.Alert"> 
    <item name="colorAccent">@color/accent_pink_a200</item> <!-- gets applied to positive/neutral/negative buttons --> 
    <item name="android:textColor">@color/white</item> 
    <item name="android:textColorPrimary">@color/white</item> 
    <item name="android:textColorSecondary">@color/white</item> <!-- gets applied to the message text--> 
    <item name="android:background">@color/primary_deepPurple_500</item> 
    <item name="textColorAlertDialogListItem">@color/white</item> <!-- if the alert's got a list instead of a message --> 
</style> 

,並在您的基礎應用主題這樣的..

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    ... 
    <item name="alertDialogTheme">@style/CustomDialogAlert</item> 
</style>