Android 中的正常警报对话框
Normal Alert Dialog in Android
警报对话框显示为自定义警报对话框,如白色对话框。如何使警告对话框显示正常?黑色和蓝色。
AlertDialog.Builder builder = new AlertDialog.Builder(this)
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Hello")
.setIcon(R.drawable.ic_launcher)
.setMessage("Hi")
.setPositiveButton("Yes", null)
.setNegativeButton("No", null)
.setNeutralButton("Maybe", null);
是否可以在不更改整个主题的情况下更改警报对话框?
您指向的对话框是 HoloTheme,上面白色的是 AppCompatMaterial 主题。所以如果你想使用蓝色和黑色,然后恢复到全息主题
AlertDialog.Builder builder = new AlertDialog.Builder(context,android.R.style.Theme_Holo_Dialog);
您必须使用支持 v7 库并确保您导入 android.support.v7.app.AlertDialog;
而不是 import android.app.AlertDialog;
您可以自定义提醒对话框。
这是你可以做的事情:
在您的 style.xml 中声明此代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AboutDialog" parent="@android:style/Theme.Dialog">
<item name="android:textColor">?android:attr/textColorPrimaryInverseDisableOnly</item>
</style>
</resources>
现在只需创建一个使用您的样式的 ContextThemeWrapper 实例,并将其传递给生成器而不是 activity 上下文。
所以,而不是像这样:
new AlertDialog.Builder(context)
…我们现在有这样的东西:
new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AboutDialog))
还有一个技巧可以让它正常工作,那就是确保在为 AlertDialog 扩展自定义布局时也使用相同的 ContextThemeWrapper。
在我们的例子中,我需要改变这个:
View view = View.inflate(context, R.layout.about_dialog, null);
……对此:
View view = View.inflate(new ContextThemeWrapper(context, R.style.AboutDialog), R.layout.about_dialog, null);
希望对您有所帮助。
试试这个
ContextThemeWrapper themedContext;
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
themedContext = new ContextThemeWrapper( this, android.R.style.Theme_Holo_NoActionBar);
}
else {
themedContext = new ContextThemeWrapper( this, android.R.style.Theme_Light_NoTitleBar );
}
AlertDialog.Builder builder = new AlertDialog.Builder(themedContext)
// your dialog code.....
警报对话框显示为自定义警报对话框,如白色对话框。如何使警告对话框显示正常?黑色和蓝色。
AlertDialog.Builder builder = new AlertDialog.Builder(this)
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Hello")
.setIcon(R.drawable.ic_launcher)
.setMessage("Hi")
.setPositiveButton("Yes", null)
.setNegativeButton("No", null)
.setNeutralButton("Maybe", null);
是否可以在不更改整个主题的情况下更改警报对话框?
您指向的对话框是 HoloTheme,上面白色的是 AppCompatMaterial 主题。所以如果你想使用蓝色和黑色,然后恢复到全息主题
AlertDialog.Builder builder = new AlertDialog.Builder(context,android.R.style.Theme_Holo_Dialog);
您必须使用支持 v7 库并确保您导入 android.support.v7.app.AlertDialog;
而不是 import android.app.AlertDialog;
您可以自定义提醒对话框。
这是你可以做的事情:
在您的 style.xml 中声明此代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AboutDialog" parent="@android:style/Theme.Dialog">
<item name="android:textColor">?android:attr/textColorPrimaryInverseDisableOnly</item>
</style>
</resources>
现在只需创建一个使用您的样式的 ContextThemeWrapper 实例,并将其传递给生成器而不是 activity 上下文。
所以,而不是像这样:
new AlertDialog.Builder(context)
…我们现在有这样的东西:
new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AboutDialog))
还有一个技巧可以让它正常工作,那就是确保在为 AlertDialog 扩展自定义布局时也使用相同的 ContextThemeWrapper。
在我们的例子中,我需要改变这个:
View view = View.inflate(context, R.layout.about_dialog, null);
……对此:
View view = View.inflate(new ContextThemeWrapper(context, R.style.AboutDialog), R.layout.about_dialog, null);
希望对您有所帮助。
试试这个
ContextThemeWrapper themedContext;
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
themedContext = new ContextThemeWrapper( this, android.R.style.Theme_Holo_NoActionBar);
}
else {
themedContext = new ContextThemeWrapper( this, android.R.style.Theme_Light_NoTitleBar );
}
AlertDialog.Builder builder = new AlertDialog.Builder(themedContext)
// your dialog code.....