private void showAlerDialog() {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("AlerDialog")
.setMessage("这是一个AlertDialog")
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create();
dialog.show();
}
need
In android, we can easily pop up one through the above code AlertDialog
, including the title, content, color and size of the button, etc. There is no method exposed in the system code to allow us to customize them, but if there is a need, in order to highlight OK, we need to change the OK button to red, what should we do, or we need to modify the title color, etc. Of course, we can choose to customize the View, we will not discuss this method here, we try to modify the native AlertDialog
title, button color, etc.;
analyze
Modify content color
If we need to modify the color of the content to be blue, how do we modify it? Since the native AlertDialog
does not provide a modification method, we can extract this control through reflection, and then modify its color;
public class AlertDialog extends AppCompatDialog implements DialogInterface {
final AlertController mAlert;
...
}
Click on the open AlertDialog
source code, we found that there is a global AlertController
, AlertDialog
mainly through this class to achieve, we continue to look at the source code of this class;
class AlertController {
...
private ImageView mIconView;
private TextView mTitleView;
private TextView mMessageView;
private View mCustomTitleView;
...
In the source code of this class, we see that there are fields mTitleView
, mMessageView
etc. These fields are what we need, and we can dynamically modify them through reflection;
private void showAlerDialog() {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("AlerDialog")
.setMessage("这是一个AlertDialog")
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create();
dialog.show();
try {
Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
mAlert.setAccessible(true);
Object mAlertController = mAlert.get(dialog);
Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
mMessage.setAccessible(true);
TextView mMessageView = (TextView) mMessage.get(mAlertController);
mMessageView.setTextColor(Color.BLUE);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
Through the above code, we can modify the color or size of the native content through reflection ;AlertDialog
Modify button color
Modifying the color of the button can also be done by reflection, but the native AlertDialog
provides the corresponding method to realize the operation of the button, so we can call it directly through the following methods, for example, modify the color of the button to black, and modify the color of the button to blue:
private void showAlerDialog() {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("AlerDialog")
.setMessage("这是一个AlertDialog")
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE);
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
}
Through the above code, you can AlertDialog
define the color of the middle button yourself;
In this way, we have completed the custom title, content, button color or size, etc. It should be noted that whether it is modified through reflection or native methods, it needs to be done after AlertDialog
the show()
method is called, otherwise an error will be reported;