Android - 更改应用程序中所有对话框的对话框标题样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11921216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - change dialog title style of all dialogs in application
提问by jaibatrik
Is there a way I can change all the alert dialogs appearing in my Android application? I want to change the dialogs that are system generated as well (like the Edit Text dialog that opens up when you long tap on any EditText). I want to change the title font color and size of all the dialogs in my app.
有没有办法可以更改出现在我的 Android 应用程序中的所有警报对话框?我也想更改系统生成的对话框(例如长按任何 EditText 时打开的 Edit Text 对话框)。我想更改应用程序中所有对话框的标题字体颜色和大小。
Is there a way to do it?
有没有办法做到这一点?
I have tried setting android:alertDialogTheme
in my theme. But it seems to be not working. Code following -
我试过android:alertDialogTheme
在我的主题中设置。但它似乎不起作用。代码如下 -
<style name="Theme.DLight" parent="android:Theme.Light.NoTitleBar">
<item name="android:alertDialogTheme">@style/DialogStyle</item>
</style>
and
和
<style name="DialogStyle" parent="android:Theme" >
<item name="android:windowBackground">@drawable/background_holo_light</item>
<item name="android:textColor">#014076</item>
</style>
EDIT
编辑
I'm not invoking a dialog from my code. It's just the default dialog that appears when you long click on any EditText. Generally it contains the keyboard options like Select word, Select all, Input method etc.
我没有从我的代码中调用对话框。它只是当您长按任何 EditText 时出现的默认对话框。通常它包含键盘选项,如选择单词、全选、输入法等。
回答by David Ferrand
In you dialog theme, the attribute you need to modify windowTitleStyle
. There, reference a style for your title and define a text appearance for this title. For example, to display your title red and bold:
在您的对话框主题中,您需要修改的属性windowTitleStyle
。在那里,为您的标题引用样式并为此标题定义文本外观。例如,要将标题显示为红色和粗体:
<style name="AppTheme" parent="@android:style/Theme.Holo">
...
<item name="alertDialogTheme">@style/AppTheme.AlertDialog</item>
</style>
<style name="DialogStyle" parent="@style/Theme.Holo.Dialog.Alert">
...
<item name="android:windowTitleStyle">@style/DialogWindowTitle_Custom</item>
</style>
<style name="DialogWindowTitle_Custom" parent="@style/DialogWindowTitle_Holo">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">@style/TextAppearance_DialogWindowTitle</item>
</style>
<style name="TextAppearance_DialogWindowTitle" parent="@android:style/TextAppearance.Holo.DialogWindowTitle">
<item name="android:textColor">@android:color/holo_red_light</item>
<item name="android:textStyle">bold</item>
</style>
If you want to style a little bit more your AlertDialog, I wrote a blog postdetailing the steps.
如果您想为 AlertDialog 设置更多样式,我写了一篇博客文章,详细介绍了这些步骤。
回答by Prajwal W
In styles.xml:
在styles.xml中:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
//All your other styles and add the one mntioned below.
<item name="alertDialogTheme">@style/AlertDialogStyle</item>
</style>
In the styles of AlertDialog add:
在 AlertDialog 的样式中添加:
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:editTextColor">@color/primary_text</item>
<item name="android:background">@color/white</item>
<item name="android:windowTitleStyle">@style/TextAppearance.AppCompat.Title</item>
</style>
windowTitleStyleis important to add as it will give your title that effect that you need.
windowTitleStyle添加很重要,因为它将为您的标题提供您需要的效果。
Now simply use this Theme in your AlertDialog in any activity that you want like below:
现在只需在您想要的任何活动中的 AlertDialog 中使用此主题,如下所示:
AlertDialog.Builder dialog = new AlertDialog.Builder(this, R.style.AlertDialogStyle);
回答by SALMAN
import android.app.ProgressDialog;
import android.content.Context;
import android.view.animation.Animation;
public class MyProgressDialog extends ProgressDialog {
public MyProgressDialog(Context context) {
super(context,R.style.NewDialog);
// TODO Auto-generated constructor stub
}
}
//RESOURCE STYLE FILE res->values->mydialog.xml
//资源样式文件 res->values->mydialog.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NewDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:colorBackground">#ffffff</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:width">30dip</item>
<item name="android:height">30dip</item>
<item name="android:textColor">#FF0000</item>
</style>
</resources>
//AND USE THIS CODE ANYWHERE IN YOUR APPLICATION TO GET CUSTOM PROGRESS DIALOG
//并在您的应用程序中的任何地方使用此代码以获取自定义进度对话框
MyProgressDialog pd = new MyProgressDialog(YouActivity.this);
pd.setMessage("Loading. Please wait...");
pd.show();
Thanks :)
谢谢 :)