设置AlertDialog的消息文本的自定义字体

insrf1ej  于 2022-09-21  发布在  Android
关注(0)|答案(2)|浏览(194)

我有一个简单的警告对话框显示在我的应用程序中,我想设置标题和消息文本的自定义字体,即使我通过添加FontFamily属性来设置样式。它不会影响消息文本(标题以某种方式获得了正确的字体)

到目前为止,我尝试的内容如下:

上下文主题 Package 器

Context context = new ContextThemeWrapper(context, R.style.NetworkAlertDialogStyle);`

AlertDialog.Builder builder = new AlertDialog.Builder(context);

风格

<style name="NetworkAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:textViewStyle">@style/MyTextViewStyle</item>
    </style>

<style name="MyTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">@font/baloo2_bold</item>
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/black</item>
</style>

将样式传递给AlertDialog构建器的构造函数

AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.NetworkAlertDialogStyle);

<style name="NetworkAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:fontFamily">@font/baloo2_regular</item>
</style>
fcy6dtqo

fcy6dtqo1#

试试这个

<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:windowBackground">@color/white</item>
        <item name="colorSurface">@color/white</item>
        <item name="android:layout_gravity">center</item>
        <item name="materialAlertDialogTitleTextStyle">@style/TitleTextStyle</item>
        <item name="materialAlertDialogBodyTextStyle">@style/BodyTextStyle</item>
        <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle1</item>
        <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle1</item>
        <item name="android:colorControlHighlight">@color/tool_icon_press</item>
    </style>
    <style name="TitleTextStyle" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
        <item name="android:textColor">@color/text_color</item>
        <item name="android:fontFamily">@font/inter_semi_bold</item>
        <item name="android:textSize">@dimen/_14sdp</item>
    </style>
    <style name="BodyTextStyle" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
        <item name="android:textColor">@color/text_color</item>
        <item name="android:fontFamily">@font/inter_semi_bold</item>
    </style>
    <style name="PositiveButtonStyle1" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">@color/text_color</item>
        <item name="rippleColor">@color/tool_icon_press</item>
        <item name="android:fontFamily">@font/inter_semi_bold</item>

    </style>
    <style name="NegativeButtonStyle1" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">@color/text_color</item>
        <item name="rippleColor">@color/tool_icon_press</item>
        <item name="android:fontFamily">@font/inter_semi_bold</item>
    </style>

像这样使用

new Dialog(context, R.style.AlertDialogTheme)
axr492tv

axr492tv2#

创建自定义对话框,然后您可以相应地设置字体系列

相关问题