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

如何实现点击单选按钮时修改WebView中HTML内容的字号?

实现WebView内容字号切换的解决方案

步骤1:定义字号常量与持久化存储

首先我们需要用SharedPreferences来保存用户的字号选择(确保重启应用后设置不会丢失),同时定义好不同字号的取值:

在HomeActivity中添加如下代码:

// 定义字号常量,可根据需求调整数值
public static final String FONT_SIZE_SMALL = "14px";
public static final String FONT_SIZE_LARGE = "18px";
private static final String PREF_KEY_FONT = "font_size_setting";

// 获取SharedPreferences实例
private SharedPreferences getFontPrefs() {
    return getSharedPreferences("AppSettings", MODE_PRIVATE);
}

// 保存用户选择的字号
private void saveFontSize(String fontSize) {
    getFontPrefs().edit().putString(PREF_KEY_FONT, fontSize).apply();
}

// 获取当前生效的字号,默认小字号
private String getCurrentFontSize() {
    return getFontPrefs().getString(PREF_KEY_FONT, FONT_SIZE_SMALL);
}

步骤2:完善弹窗的单选按钮交互逻辑

在你已有的弹窗初始化代码后,添加单选按钮的选中监听,处理用户的字号切换请求:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.font_change) { 
        dialog = new Dialog(HomeActivity.this); 
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        dialog.setContentView(R.layout.font_change_dialog); 
        dialog.show(); 
        dialog.setCanceledOnTouchOutside(true); 

        // 获取弹窗里的单选组和按钮
        RadioGroup fontRadioGroup = dialog.findViewById(R.id.font_radio_group);
        RadioButton smallBtn = dialog.findViewById(R.id.small);
        RadioButton largeBtn = dialog.findViewById(R.id.large);

        // 根据当前设置预选中对应按钮
        String currentSize = getCurrentFontSize();
        if (currentSize.equals(FONT_SIZE_LARGE)) {
            largeBtn.setChecked(true);
        } else {
            smallBtn.setChecked(true);
        }

        // 设置单选按钮选中事件
        fontRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            String targetSize = FONT_SIZE_SMALL;
            if (checkedId == R.id.large) {
                targetSize = FONT_SIZE_LARGE;
            }
            // 保存设置
            saveFontSize(targetSize);
            // 通知章节详情页刷新内容
            Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.your_fragment_container_id);
            if (currentFragment instanceof ChapterDetailFragment) {
                ((ChapterDetailFragment) currentFragment).refreshContentFont();
            }
            dialog.dismiss();
        });
    }
    return super.onOptionsItemSelected(item);
}

注意:请确保你的font_change_dialog.xml布局中,包含一个id为font_radio_group的RadioGroup,以及id为small和large的两个RadioButton。

步骤3:修改适配器,动态注入字号样式

在ParagraphAdapter加载HTML内容时,给内容包裹一个带字号样式的容器,实现动态调整:

// 获取当前生效的字号(这里假设Adapter的上下文是HomeActivity,也可直接通过SharedPreferences获取)
String currentFontSize = ((HomeActivity) mContext).getCurrentFontSize();

// 给原始HTML内容添加字号样式包裹
String styledHtml = "<div style='font-size:" + currentFontSize + "; line-height:1.6;'>"
        + getItem(position - 1).getContent()
        + "</div>";

mViewHolder.mChapterContextTextView.loadData(styledHtml, "text/html; charset=UTF-8;", null);

步骤4:在ChapterDetailFragment中添加刷新方法

为了让用户切换字号后立即看到效果,在ChapterDetailFragment中添加刷新适配器的方法:

public void refreshContentFont() {
    // 假设你的适配器实例是mParagraphAdapter
    if (mParagraphAdapter != null) {
        mParagraphAdapter.notifyDataSetChanged();
    }
}

可选简化方案:直接控制WebView缩放

如果你的HTML内容是纯文本类,也可以直接通过WebView的设置调整文本缩放比例,无需修改HTML内容:

// 小字号对应80%缩放,大字号对应120%缩放,可按需调整
if (targetSize.equals(FONT_SIZE_LARGE)) {
    webView.getSettings().setTextZoom(120);
} else {
    webView.getSettings().setTextZoom(80);
}

内容的提问来源于stack exchange,提问作者anju jo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:15:41