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

如何修改Sweet Alert Dialog库中.setTitleText()等方法的文本字体大小?

嘿,我对Sweet Alert Dialog这个库挺熟悉的,你遇到的字体大小调整问题其实有几种实用的解决办法,分情况给你唠唠:

解决Sweet Alert Dialog字体大小调整的两种方法

方法一:使用自定义布局(最稳定灵活)

这种方法是最推荐的,完全不受库本身的限制,你可以自定义任何样式:

  1. 先创建一个自定义布局文件(比如res/layout/custom_sweet_alert.xml),在里面定义标题和内容的TextView,直接设置你想要的字体大小:
<?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="24dp">

    <TextView
        android:id="@+id/tv_custom_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textStyle="bold"
        android:textColor="@color/black"
        android:layout_gravity="center_horizontal"/>

    <TextView
        android:id="@+id/tv_custom_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@color/dark_gray"
        android:layout_marginTop="12dp"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>
  1. 在代码中加载这个布局,替换Sweet Alert的默认内容区域:
SweetAlertDialog dialog = new SweetAlertDialog(this, SweetAlertDialog.NORMAL_TYPE);

// 加载自定义布局
View customView = LayoutInflater.from(this).inflate(R.layout.custom_sweet_alert, null);
TextView titleTv = customView.findViewById(R.id.tv_custom_title);
TextView contentTv = customView.findViewById(R.id.tv_custom_content);

// 设置你需要的文本
titleTv.setText("自定义标题");
contentTv.setText("这里是自定义的内容文本,字体大小完全由你控制");

// 替换弹窗的默认布局
dialog.setCustomView(customView);
dialog.show();

方法二:通过反射修改默认控件(快速临时方案)

如果你不想写自定义布局,可以用反射获取库内部的标题和内容TextView,直接修改字体大小。不过要注意:如果库后续更新修改了内部字段名,这个方法会失效,所以只适合临时快速解决:

SweetAlertDialog dialog = new SweetAlertDialog(this, SweetAlertDialog.NORMAL_TYPE)
        .setTitleText("默认标题")
        .setContentText("默认内容");

// 修改标题字体大小
try {
    Field titleField = SweetAlertDialog.class.getDeclaredField("mTitleText");
    titleField.setAccessible(true);
    TextView titleTv = (TextView) titleField.get(dialog);
    titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
} catch (NoSuchFieldException | IllegalAccessException e) {
    e.printStackTrace();
}

// 修改内容字体大小
try {
    Field contentField = SweetAlertDialog.class.getDeclaredField("mContentText");
    contentField.setAccessible(true);
    TextView contentTv = (TextView) contentField.get(dialog);
    contentTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
} catch (NoSuchFieldException | IllegalAccessException e) {
    e.printStackTrace();
}

dialog.show();

补充:如果是Web端的SweetAlert2

要是你用的是Web版本的SweetAlert2,直接通过CSS就能修改:

/* 修改标题字体大小 */
.swal2-title {
    font-size: 22px !important;
}

/* 修改内容字体大小 */
.swal2-content {
    font-size: 16px !important;
}

内容的提问来源于stack exchange,提问作者اندرولند androland

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:26:59