Change dialog button color

后端 未结 2 1386
暖寄归人
暖寄归人 2020-12-03 23:59

Is there any way how to change default dialog buttons colour, or do I need to do custom dialog for that?

This is my dialog:

private void removeItem(f         


        
相关标签:
2条回答
  • 2020-12-04 00:57

    You can use the MaterialAlertDialogBuilder provided by the Material Components library which allows the creation of a Material AlertDialog.

    Just use:

       new MaterialAlertDialogBuilder(MainActivity.this,
           R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
                  .setTitle("Dialog")
                  .setMessage("Lorem ipsum dolor ....")
                  .setPositiveButton("Ok", /* listener = */ null)
                  .setNegativeButton("Cancel", /* listener = */ null)
                  .show();
    

    Then define your custom style, using the buttonBarPositiveButtonStyle and buttonBarNegativeButtonStyle attributes:

      <!-- Alert Dialog -->
      <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
         <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
         <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
         <item name="buttonBarNeutralButtonStyle">....</item>
      </style>
    
    
      <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">#FFFFFF</item>
        <item name="backgroundTint">#00f</item>
      </style>
    
      <style name="NegativeButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">@color/colorAccent</item>
        <item name="backgroundTint">@color/secondaryColor</item>
      </style>
    

    0 讨论(0)
  • 2020-12-04 00:59

    you can give style to your alert dialog like this:

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle);
    

    And you can write this style in xml file like this:

    <style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:colorAccent">#f3f3f3</item>
        <item name="android:textColor">#f3f3f3</item>
        <item name="android:textColorPrimary">#f3f3f3</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题