/**
* A sublcass of {@link AlertDialog} used to customize the title and title
* divider colors
*/
public class CustomDialog extends AlertDialog {
/**
* Constructor for <code>CustomDialog</code>
*
* @param context The {@link Context} to use
*/
public CustomDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Resources res = getContext().getResources();
final int yellow = res.getColor(android.R.color.holo_orange_light);
// Title
final int titleId = res.getIdentifier("alertTitle", "id", "android");
final View title = findViewById(titleId);
if (title != null) {
((TextView) title).setTextColor(yellow);
}
// Title divider
final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
final View titleDivider = findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(yellow);
}
}
}
实现
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final CustomDialog customDialog = new CustomDialog(this);
customDialog.setTitle("Title");
customDialog.setMessage("Message");
customDialog.show();
}
使用 DialogFragment
和 AlertDialog.Builder
public class CustomDialogFragment extends DialogFragment {
/**
* Empty constructor as per the {@link Fragment} docs
*/
public CustomDialogFragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Title")
.setMessage("Message")
.create();
}
@Override
public void onStart() {
super.onStart();
final Resources res = getResources();
final int yellow = res.getColor(android.R.color.holo_orange_light);
// Title
final int titleId = res.getIdentifier("alertTitle", "id", "android");
final View title = getDialog().findViewById(titleId);
if (title != null) {
((TextView) title).setTextColor(yellow);
}
// Title divider
final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
final View titleDivider = getDialog().findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(yellow);
}
}
}
实现
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new CustomDialogFragment().show(getFragmentManager(), "customDialogFragment");
}
结果
DialogFragment
和AlertDialog.Builder
提供了一个示例。 - adneal