我编写了显示弹出对话框的 android 代码,但我想将背景颜色从黑色更改为白色,然后更改文字颜色。
这是对话框的代码:
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);
if (!welcomeScreenShown) {
String whatsNewText = getResources().getString(R.string.Text);
new AlertDialog.Builder(this).setMessage(whatsNewText).setPositiveButton(
R.string.ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit(); // Very important to save the preference
}
要扩展 @DaneWhite 的答案,您不必依赖内置主题。您可以轻松提供自己的风格:
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/myColor</item>
</style>
然后将其应用到 Builder 构造函数中:
Java:
AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
...
.create();
Kotlin:
var alertDialog = AlertDialog.Builder(context, R.style.MyDialogTheme)
...
.create()
无论您是否使用,这都应该有效android.support.v7.app.AlertDialog
or android.app.AlertDialog
这也比 @DummyData 的答案更好,因为您没有调整对话框的大小。如果您设置窗口的背景可绘制,您将覆盖一些现有的尺寸信息并获得一个非标准宽度的对话框。
如果您在主题上设置背景并在对话框上设置主题,您最终会得到一个对话框,该对话框的颜色如您所愿,但宽度仍然正确。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)