Set AlertBox Title Bar Background Color

后端 未结 3 738
囚心锁ツ
囚心锁ツ 2021-02-14 21:11

How can I change the background color for an alertbox\'s title bar?

AlertDialog.Builder alert=new AlertDialog.Builder(getParent());
alert.setTitle(\"sample\");
a         


        
3条回答
  •  遥遥无期
    2021-02-14 21:51

    From the answer @CornflakesDK and @ice spirit, I thought you can use the current AlertDialog.Builder implementation to do the custom dialog and make it easy to maintain.

    CustomDialogBuilder.java

    public class CustomDialogBuilder extends AlertDialog.Builder {
    
      private View view;
    
      public CustomDialogBuilder(Context context) {
        super(context);
        view = LayoutInflater.from(getContext()).inflate(R.layout.custom_dialog_title, null);
        setCustomTitle(view);
      }
    
      @Override
      public Builder setTitle(int titleId) {
        TextView titleTextView = view.findViewById(R.id.exemptionSubHeading4);
        titleTextView.setText(getContext().getString(titleId));
        return this;
      }
    
      @Override
      public Builder setTitle(CharSequence title) {
        TextView titleTextView = view.findViewById(R.id.exemptionSubHeading4);
        titleTextView.setText(title);
        return this;
      }
    }
    

    custom_dialog.xml

    
    
      
        
      
    
    

    Inside your activity code,

     new CustomDialogBuilder(MyActivity.this)
                      .setTitle(R.string.actions)
                      .setItems(R.array.items_actions, (dialog, which) -> {
                        // handle items
                      }).create().show();
    

    Then, you can have styling inside the DialogBuilder and also utilize the functions of the AlertDialog.Builder.

提交回复
热议问题