You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Android AlertDialog的标题和消息文本设置自定义字体?

解决AlertDialog消息文本自定义字体不生效的问题

方法一:自定义AlertDialog布局

直接通过自定义布局完全控制标题和消息的字体样式,这种方式最稳妥,不会出现样式覆盖失效的情况。

  1. 创建自定义布局文件 dialog_custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <!-- 标题文本 -->
    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/baloo2_bold"
        android:textSize="18sp"
        android:textColor="@color/black"
        android:layout_marginBottom="8dp"/>

    <!-- 消息文本 -->
    <TextView
        android:id="@+id/dialog_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/baloo2_regular"
        android:textSize="16sp"
        android:textColor="@color/black"/>

    <!-- 按钮区域(也可以保留Builder的按钮设置逻辑) -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="end"
        android:layout_marginTop="16dp">

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定"/>
    </LinearLayout>
</LinearLayout>
  1. 在代码中加载布局并设置内容:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_custom, null);
builder.setView(dialogView);

// 绑定控件并设置文本
TextView titleTv = dialogView.findViewById(R.id.dialog_title);
TextView messageTv = dialogView.findViewById(R.id.dialog_message);
titleTv.setText("自定义标题");
messageTv.setText("这是应用了自定义字体的消息文本");

AlertDialog dialog = builder.create();
dialog.show();

// 设置按钮点击事件
Button positiveBtn = dialogView.findViewById(R.id.dialog_positive_btn);
positiveBtn.setOnClickListener(v -> dialog.dismiss());

方法二:直接获取消息TextView设置字体

如果不想用自定义布局,可以在Dialog显示后,找到消息文本控件手动赋值字体。

AlertDialog dialog = new AlertDialog.Builder(context)
        .setTitle("标题")
        .setMessage("待设置字体的消息文本")
        .setPositiveButton("确定", null)
        .create();

dialog.show();

// 找到消息文本控件并设置字体
TextView messageTextView = dialog.findViewById(android.R.id.message);
if (messageTextView != null) {
    Typeface messageTypeface = ResourcesCompat.getFont(context, R.font.baloo2_regular);
    messageTextView.setTypeface(messageTypeface);
    // 可选:同步调整字号、颜色
    messageTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    messageTextView.setTextColor(ContextCompat.getColor(context, R.color.black));
}

// 标题需要单独设置的话也可以用同样方式
TextView titleTextView = dialog.findViewById(R.id.alertTitle);
if (titleTextView != null) {
    Typeface titleTypeface = ResourcesCompat.getFont(context, R.font.baloo2_bold);
    titleTextView.setTypeface(titleTypeface);
}

为什么之前的方法失效?

AlertDialog的标题和消息文本对应的TextView,在系统主题中使用了不同的样式属性。标题可能继承了全局的textViewStyle,但消息文本有专属的样式配置(比如alertDialogMessageStyle),直接全局设置textViewStyle无法覆盖这个专属样式,导致消息字体不生效。


内容的提问来源于stack exchange,提问作者Zafer Celaloglu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 23:20:27