Ric*_*ick 40 android background-color android-alertdialog
我编写了一个显示弹出对话框的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
}
Run Code Online (Sandbox Code Playgroud)
tir*_*r38 77
要扩展@DaneWhite的答案,您不必依赖内置主题.您可以轻松提供自己的风格:
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/myColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后将其应用于Builder构造函数:
AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
...
.create();
Run Code Online (Sandbox Code Playgroud)
无论您是使用android.support.v7.app.AlertDialog
还是使用,这都应该有效android.app.AlertDialog
这也比@ DummyData的答案更好,因为你没有调整对话框的大小.如果设置窗口的背景可绘制,则覆盖一些现有的尺寸信息并获得非标准宽度的对话框.
如果你在主题上设置背景并在对话框上设置主题,你最终会得到一个对话框,它会按照你想要的颜色,但仍然是正确的宽度.
Dan*_*ite 58
如果您只想要一个浅色主题而不是特定颜色,那么您可以将主题ID传递给AlertDialog.Builder构造函数.
AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT)...
Run Code Online (Sandbox Code Playgroud)
要么
AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)...
Run Code Online (Sandbox Code Playgroud)
Dum*_*ata 31
幸得苏希尔
像往常一样创建AlertDialog:
AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
Dialog dialog = dialog.create();
dialog.show();
Run Code Online (Sandbox Code Playgroud)
在对话框上调用show()后,设置背景颜色如下:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
Run Code Online (Sandbox Code Playgroud)
Gab*_*tti 13
使用材料组件库,您可以只使用默认值MaterialAlertDialogBuilder
:
new MaterialAlertDialogBuilder(AlertDialogActivity.this,
R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog_Background)
.setTitle("Dialog")
.setMessage("Message... ....")
.setPositiveButton("Ok", /* listener = */ null)
.show();
Run Code Online (Sandbox Code Playgroud)
主题叠加在哪里ThemeOverlay_MaterialComponents_MaterialAlertDialog_Background
:
<!-- Alert Dialog -->
<style name="ThemeOverlay.MaterialComponents.MaterialAlertDialog_Background" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Background Color-->
<item name="android:background">@color/.....</item>
<!-- Text Color for title and message -->
<item name="colorOnSurface">@color/......</item>
<!-- Style for positive button -->
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
<!-- Style for negative button -->
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
</style>
<style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button">
<!-- text color for the button -->
<item name="android:textColor">@color/.....</item>
<!-- Background tint for the button -->
<item name="backgroundTint">@color/primaryDarkColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)
kik*_*283 11
对于任何myDialog
调用的对话框,调用后myDialog.show();
可以调用:
myDialog.getWindow().getDecorView().getBackground().setColorFilter(new LightingColorFilter(0xFF000000, CUSTOM_COLOR));
Run Code Online (Sandbox Code Playgroud)
其中CUSTOM_COLOR
是8位十六进制格式,例如 0xFF303030
.这里FF
是alpha值,其余是十六进制的颜色值.
要更改应用程序中所有对话框和弹出窗口的背景颜色,请使用colorBackgroundFloating
属性。
<style name="MyApplicationTheme" parent="@style/Theme.AppCompat.NoActionBar">
...
<item name="colorBackgroundFloating">
@color/background</item>
<item name="android:colorBackgroundFloating" tools:targetApi="23">
@color/background</item>
...
</style>
Run Code Online (Sandbox Code Playgroud)
说明文件:
归档时间: |
|
查看次数: |
93186 次 |
最近记录: |