如何修改AlertDialog的标题和标题下面的分割线的颜色
效果棒棒哒。
效果 :

1.反射
public static void colorAlertDialog1(Context context, AlertDialog dialog) {
try {
int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
if (dividerId != 0) {
View divider = dialog.findViewById(dividerId);
divider.setBackgroundColor(context.getResources().getColor(R.color.text_grey));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 2);
lp.setMargins(20, 0, 20, 0);
divider.setLayoutParams(lp);
}
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
if (textViewId != 0) {
TextView tv = (TextView) dialog.findViewById(textViewId);
tv.setTextColor(context.getResources().getColor(R.color.red));
}
} catch (Exception e) {
return;
}
}
该方法的问题:对于国内深度定制的rom。。。该方法无效。而且,如果没有trycatch的话,还会导致异常。2.继承AlertDialogBuilder
public class CustomAlertDialogBuilder extends AlertDialog.Builder {
/** The custom_body layout */
private View mDialogView;
private AlertDialog mDialog;
/** optional dialog title layout */
private TextView mTitle;
/** optional alert dialog image */
private ImageView mIcon;
/** optional message displayed below title if title exists */
private TextView mMessage;
/**
* method
*/
/** optional custom panel image */
private FrameLayout mCustom;
/** Keep Context as Member to support Apis below 11 */
private Context mContext;
public CustomAlertDialogBuilder(Context context, int theme) {
super(context, theme);
mDialogView = View.inflate(context, R.layout.dialog_custom_layout, null);
super.setView(mDialogView);
mTitle = (TextView) mDialogView.findViewById(R.id.alertTitle);
mMessage = (TextView) mDialogView.findViewById(R.id.message);
mIcon = (ImageView) mDialogView.findViewById(R.id.icon);
mCustom = (FrameLayout) mDialogView.findViewById(R.id.customPanel);
mContext = context;
}
/**
* Use this method to reference the whole dialog view, i.e. when inflating a
* custom view with {@link #setCustomView(int, Context)} and you want to
* find an id from the inflated layout.
*
* example:
*
* CheckBox showAgain = (CheckBox)
* myQustomDialogBuilder.getDialogView().findViewById
* (R.id.inflated_checkbox); if (!showAgain.isChecked()) { // do something }
*
* @return the qustom dialog view
*/
public View getDialogView() {
return mDialogView;
}
@Override
public CustomAlertDialogBuilder setTitle(CharSequence text) {
mDialogView.findViewById(R.id.topPanel).setVisibility(View.VISIBLE);
mTitle.setText(text);
return this;
}
@Override
public CustomAlertDialogBuilder setTitle(int textResId) {
mDialogView.findViewById(R.id.topPanel).setVisibility(View.VISIBLE);
mTitle.setText(textResId);
return this;
}
@Override
public CustomAlertDialogBuilder setMessage(int textResId) {
mDialogView.findViewById(R.id.contentPanel).setVisibility(View.VISIBLE);
mMessage.setText(textResId);
return this;
}
@Override
public CustomAlertDialogBuilder setMessage(CharSequence text) {
mDialogView.findViewById(R.id.contentPanel).setVisibility(View.VISIBLE);
mMessage.setText(text);
return this;
}
@Override
public CustomAlertDialogBuilder setIcon(int drawableResId) {
mIcon.setVisibility(View.VISIBLE);
mIcon.setImageResource(drawableResId);
return this;
}
@Override
public CustomAlertDialogBuilder setIcon(Drawable icon) {
mIcon.setImageDrawable(icon);
return this;
}
/**
* This allows you to specify a custom layout for the area below the title
* divider bar in the dialog. As an example you can look at
* example_ip_address_layout.xml and how I added it in
* TestDialogActivity.java
*
* @param resId
* of the layout you would like to add
* @param context
*/
@Override
public CustomAlertDialogBuilder setView(View view) {
mCustom.setVisibility(View.VISIBLE);
mCustom.addView(view);
return this;
}
@Override
public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final DialogInterface.OnClickListener listener) {
final ListView listView = (ListView) mDialogView.findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setItemChecked(checkedItem, true);
if (listener != null) {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listener.onClick(CustomAlertDialogBuilder.this.mDialog, position);
}
});
}
return this;
}
@Override
public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final DialogInterface.OnClickListener listener) {
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(mContext, android.R.layout.simple_list_item_single_choice, android.R.id.text1,
items);
return this.setSingleChoiceItems(adapter, checkedItem, listener);
}
@Override
public Builder setSingleChoiceItems(int itemsId, int checkedItem, final DialogInterface.OnClickListener listener) {
return this.setSingleChoiceItems(mContext.getResources().getTextArray(itemsId), checkedItem, listener);
}
}
这样,使用时
AlertDialog dialog = new CustomAlertDialogBuilder(mActivity, R.style.theDialog).setTitle(R.string.tips).setMessage(title)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finishAnim();
}
}).create();
dialog.setCancelable(false);
dialog.show();
效果棒棒哒。
如何修改AlertDialog按钮的背景色和字体颜色
自定义style
<style name="theDialog" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowFrame">@null</item>
<item name="android:borderlessButtonStyle">@style/alertdialogbtn</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:backgroundDimAmount">0.5</item>
</style>
关键一行是<item name="android:borderlessButtonStyle">@style/alertdialogbtn</item>
<style name="alertdialogbtn" parent="@android:style/Widget.Holo.Button.Borderless">
<item name="android:background">@drawable/btn_bg_dialog</item>
<item name="android:textColor">@color/text</item>
</style>
效果 :