在AlertDialog中更改按钮颜色

15 浏览
0 Comments

在AlertDialog中更改按钮颜色

在Android中,我怎么样可以改变一个 AlertDialog 中的按钮的颜色?

admin 更改状态以发布 2023年5月21日
0
0 Comments

由于大多数人现在可能正在使用DialogFragment,我遇到了一些问题,通过阅读几篇SO答案来解决这些问题。让我发布一下我的当前解决方案。

最终,我采用了自定义可绘制对象来设置按钮背景,这也已经多次建议过。然而,在DialogFragmentonCreateDialog方法中还不可能这么做。您可以在onStart()中这样做,或者(这是我更喜欢的方式)在对话框的onShow侦听器中这样做!不过,请记住,然后需要使更改无效化您的按钮。

至于填充:简单地在可绘制对象XML的按钮中删除填充。

#在DialogFragment中的onCreateDialog:

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  // setup your dialog here...
  builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
    @Override
    public void onClick(final DialogInterface dialog, final int which) {
      // do something
    }
  });
  builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
    @Override
    public void onClick(final DialogInterface dialog, final int which) {
      // do something
    }
  });
  final AlertDialog dialog = builder.create();
  dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(final DialogInterface dialog) {
      Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
      Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE);
      // this not working because multiplying white background (e.g. Holo Light) has no effect
      //negativeButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
      final Drawable negativeButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_red);
      final Drawable positiveButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_green);
      if (Build.VERSION.SDK_INT >= 16) {
        negativeButton.setBackground(negativeButtonDrawable);
        positiveButton.setBackground(positiveButtonDrawable);
      } else {
        negativeButton.setBackgroundDrawable(negativeButtonDrawable);
        positiveButton.setBackgroundDrawable(positiveButtonDrawable);
      }
      negativeButton.invalidate();
      positiveButton.invalidate();
    }
  });
  return dialog;
}

一个按钮的Drawable-XML示例:



  
    
      
    
  
  
    
      
    
  
  
    
      
    
  

别忘了在res\values\colors.xml中定义您的颜色,例如这样(我不想要渐变,因此颜色1&2是相同的):



  #b4099930
  #b4099930
  #96099930
  #96099930
  #96099930
  #96099930

0
0 Comments

我是这样做的。

AlertDialog.Builder customBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this,android.R.style.Theme_Dialog));
customBuilder.setTitle(R.string.popup_error_title);
customBuilder.setNegativeButton("Exit application", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int which) {  
        MyActivity.this.finish();
    }  
});
AlertDialog dialog = customBuilder.create();
dialog.show();
Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null) {
    b.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button));
}

我在这里找到了可绘制的资源 (链接)

0