Change color of window frame
How do I go about changing t开发者_JS百科he color of the frame of this dialog? I've tried a bunch of things and nothing works.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
You mean white frame ? I think it's a part of 9-patch drawable you can look up how Theme.Dialog is build in the SDK_FOLDER\platforms\android-sdkversion\data\res\values and then styles.xml and themes.xml
As i've said, the white frame is a part of the background image. its panel_background.9.png So if you want to change frame - you'll need different background image + need to overwrite in style setting it.
<item name="android:windowBackground">@android:drawable/panel_background</item>
and you'll need to define a style that will be derived from Theme.Dialog and have this
<item name="android:windowBackground">@drawable/your_drawable</item>
so if you put in styles.xml something like
<style name="NewBorderDialogTheme" parent="android:Theme.Dialog">
<item name="android:windowBackground">@drawable/your_drawable</item>
</style>
Put new drawable and set your activity to the new theme - you should see your new border
If you want to do it Programatically, try below code:
What you have to do:
When
AlertDialog
is visible on your screen,OnShowListener
is called. So, by addingdialog.setOnShowListener(this)
you will be able to customize yourAlertDialog
.
Code:
// Create AlertDialog
AlertDialog.Builder adb = new AlertDialog.Builder(context1);
adb.setTitle(context1.getString(R.string.app_name))
.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = adb.create();
// Make some UI changes for AlertDialog
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
// Add or create your own background drawable for AlertDialog window
Window view = ((AlertDialog)dialog).getWindow();
view.setBackgroundDrawableResource(R.drawable.your_drawable);
// Customize POSITIVE, NEGATIVE and NEUTRAL buttons.
Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE);
positiveButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
positiveButton.setTypeface(Typeface.DEFAULT_BOLD);
positiveButton.invalidate();
Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
negativeButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
negativeButton.setTypeface(Typeface.DEFAULT_BOLD);
negativeButton.invalidate();
Button neutralButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEUTRAL);
neutralButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
neutralButton.setTypeface(Typeface.DEFAULT_BOLD);
neutralButton.invalidate();
}
});
精彩评论