如何在android中为警报对话框设置自定义字体?

Ind*_*Boy 29 android android-layout

在我的Android应用程序中,单击按钮后会出现一个警告对话框.我想为警报设置自定义字体.我在网上搜索了一些关于这个主题的教程和问题,但它们都不适合我.

我该如何更改字体?

谢谢

mik*_*t49 74

要执行此操作,请使用警报构建器来构建警报.然后,您可以从此警报中获取TextView,然后设置警报的字体.

AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/FONT"); 
textView.setTypeface(face); 
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这在API 20上抛出一个空指针异常,找不到textView. (13认同)
  • 这很好,记得一旦你获得特权就批准和upvote,以便其他人也可以得到这个帮助. (2认同)
  • 如果你想改变标题的字体,我可以用`android.support.v7.appcompat.R.id.alertTitle'来改变它. (2认同)

Cer*_*lin 19

以上答案对我没有用.

我使用了以下方法

// Initializing the alertDialog
AlertDialog alertDialog = new AlertDialog.Builder(QuizActivity.this).create();
alertDialog.setTitle("Warning");
alertDialog.setMessage("Are you sure you want to exit?");
alertDialog.show(); // This should be called before looking up for elements


// Getting the view elements
TextView textView = (TextView) alertDialog.getWindow().findViewById(android.R.id.message);
TextView alertTitle = (TextView) alertDialog.getWindow().findViewById(R.id.alertTitle);
Button button1 = (Button) alertDialog.getWindow().findViewById(android.R.id.button1);
Button button2 = (Button) alertDialog.getWindow().findViewById(android.R.id.button2);

// Setting font
textView.setTypeface(FontHelper.getFont(Fonts.MULI_REGULAR));
alertTitle.setTypeface(FontHelper.getFont(Fonts.MULI_REGULAR));
button1.setTypeface(FontHelper.getFont(Fonts.MULI_BOLD));
button2.setTypeface(FontHelper.getFont(Fonts.MULI_BOLD));
Run Code Online (Sandbox Code Playgroud)

测试7.1.1

注意:确保在显示后显示元素dialog.没有这个你就会得到NullPointerException


小智 9

我知道这是一个古老的问题,但是对于那些仍在寻找解决方案的人,我将其留在这里。

如果您只想更改文本格式,则可以覆盖alertDialogTheme属性以更改的主题AlertDialog

示例,使用Application主题

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <!-- This will override the Alert Dialog theme -->
    <item name="alertDialogTheme">@style/MyAlertDialogTheme</item>
</style>

<style name="MyAlertDialogTheme" parent="@android:style/Theme.Material.Light.Dialog.Alert">
    <item name="android:textAppearanceSmall">@style/MyTextAppearanceSmall</item>
    <item name="android:textAppearanceMedium">@style/MyTextAppearanceMedium</item>
    <item name="android:textAppearanceLarge">@style/MyTextAppearanceLarge</item>
</style>

<style name="MyTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:fontFamily">@font/comic_sans</item>
</style>
(...)
Run Code Online (Sandbox Code Playgroud)

如果我没记错的话,android:textAppearanceSmall则使用该消息和android:textAppearanceMedium该标题。但是您可以选择所需的任何内容,然后删除其余的内容。

另外一个选项

不覆盖alertDialogTheme,而是通过构建器构造函数设置样式。例:AlertDialog.Builder(getActivity(), R.style.MyAlertDialogTheme)


Use*_*ase 5

如果您正在使用Material Components,您可以通过为其声明样式来自定义您的对话框以满足几乎所有需求。例如,我为对话框创建的自定义样式:

    <style name="ThemeOverlay.App.MaterialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
        <item name="materialAlertDialogTitleTextStyle"><!--here goes your title text style --></item>
        <item name="materialAlertDialogBodyTextStyle"><!--here goes your message text style --></item>
        <item name="colorPrimary"><!--here goes your dialog primary color. e.g. button text color, etc.--></item>
        <item name="shapeAppearanceOverlay">@style/ShapeAppearance.App.SmallComponent</item> <!-- your custom shape appearance for your dialog. In my case, I am changing corner radius of dialog to rounded 20dp corners-->
        <item name="colorSurface">@color/white</item>
        <item name="buttonBarPositiveButtonStyle">@style/Widget.App.Button</item> <!-- your custom positive button style-->
        <item name="buttonBarNegativeButtonStyle">@style/Widget.App.Button</item> <!-- your custom negtive button style-->
    </style>

    <style name="ShapeAppearance.App.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">20dp</item>
    </style>

    <style name="Widget.App.Button" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="shapeAppearance">@style/ShapeAppearance.App.SmallComponent</item>
        <item name="android:textAppearance">@style/Roboto.Bold.Small</item>
        <item name="android:textColor">@color/colorAccent</item>
        <item name="android:textAllCaps">true</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

最后,在创建对话框时,不要忘记设置以下样式:

 MaterialAlertDialogBuilder(this, R.style.ThemeOverlay_App_MaterialAlertDialog)
            .setMessage("your message")
            .show()
Run Code Online (Sandbox Code Playgroud)