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

Android Gallery选字体跳转返回后切换普通字体不生效问题

问题根因

bug由三个逻辑漏洞共同导致:

  1. 状态残留:选择普通字体时,没有清空上一次选中霓虹字体时赋值的NeontypeFaceName变量,跳转页面时两个字体参数字段同时携带有效值,后续加载逻辑会优先读取霓虹字体参数,覆盖普通字体的设置。
  2. 赋值缺失:普通字体Gallery的点击监听中,仅给预览用的externalFont变量赋值,没有给用于跨页面传参的typeFaceName变量赋值,导致传过去的普通字体参数要么是null,要么是之前留存的旧值。
  3. 逻辑冗余冲突:FirstActivity中字体加载逻辑重复、判断顺序混乱,切回普通字体时没有清除霓虹效果专属的阴影层,即使字体加载正确也会残留霓虹效果。
修复步骤
  • 补全普通字体选择逻辑,选中普通字体时清空霓虹字体状态
font_style_ga.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int position, long arg3) {
        if (IsNativeAdVisible) {
            return;
        }
        // 选中普通字体时,清空霓虹字体的状态标记
        NeontypeFaceName = null;
        // 清除预览页的霓虹阴影效果
        text_edt.setShadowLayer(0,0,0,Color.TRANSPARENT);
        String fontPath = "";
        if (position == 0) {
            fontPath = "Creator_Campotype_smcp.otf";
        } else if (position == 1) {
            fontPath = "Gangnam.ttf";
        }
        externalFont = Typeface.createFromAsset(getAssets(), fontPath);
        // 给跨页面传参的变量赋值,避免传空或旧值
        typeFaceName = fontPath;
        // 实时预览选中的普通字体
        text_edt.setTypeface(externalFont);
    }
});
  • 补全霓虹字体选择逻辑,选中霓虹字体时清空普通字体状态
neon_font_ga.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (IsNativeAdVisible) {
            return;
        }
        // 选中霓虹字体时,清空普通字体的状态标记
        typeFaceName = null;
        String neonFontPath = neonTypeList[position];
        neonFonts=Typeface.createFromAsset(getAssets(), neonFontPath);
        NeontypeFaceName= neonFontPath;
        text_edt.setTextSize(textsize);
        text_edt.setTypeface(neonFonts);
        // 给预览页加霓虹阴影效果
        text_edt.setShadowLayer(11,2,-2,textcolor);
    }
});
  • 重构FirstActivity的字体加载逻辑,去掉冗余判断,互斥处理两类字体
if (bundleIntent != null) {
    // 一次性取出所有参数,避免重复调用getExtras()
    Bundle extras = bundleIntent.getExtras();
    adText.setText(extras.getString("text"));
    float textSize = extras.getFloat("textsize");
    adText.setTextSize(textSize);
    int color = extras.getInt("textcolor");
    if (color == 0) {
        color = Color.BLACK;
    }
    adText.setTextColor(color);

    String normalFontPath = extras.getString("typeface");
    String neonFontPath = extras.getString("neonTypeface");

    // 按优先级加载:霓虹字体 > 普通字体 > 系统默认字体
    if (neonFontPath != null) {
        Typeface neonTypeface = Typeface.createFromAsset(getAssets(), neonFontPath);
        adText.setTypeface(neonTypeface);
        adText.setShadowLayer(11,2,-2,color);
    } else if (normalFontPath != null) {
        Typeface normalTypeface = Typeface.createFromAsset(getAssets(), normalFontPath);
        adText.setTypeface(normalTypeface);
        // 切回普通字体必须清除霓虹阴影
        adText.setShadowLayer(0,0,0,Color.TRANSPARENT);
    } else {
        adText.setTypeface(Typeface.DEFAULT);
        adText.setShadowLayer(0,0,0,Color.TRANSPARENT);
    }
}
额外注意点
  • 两类字体的状态标记是完全互斥的,选中其中一类时必须把另一类的标记变量置空,不要留存旧值
  • 预览页和最终展示页的效果逻辑要保持一致,避免出现预览和最终结果不符的问题
  • 跨页面传参前最好做一次非空校验,避免加载不存在的字体文件触发崩溃

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 20:06:28