如何通过捆绑包传递字体?

How to pass Typeface through bundle?

我想知道如何通过捆绑包添加字体?

字体似乎不可打包或序列化,因此无法放入捆绑包中。

我找到了使用 putInt()getInt() 传递字体哈希码的提示,但我不知道它是否是一个干净的代码。

抱歉我的英语不好,谢谢你的帮助

编辑:

这里有一些详细信息可以帮助您了解我搜索的目的:

我有两个片段:

  1. 第一个是我可以更改字体的 EditText。
  2. 第二,我有一个带有字体列表的 recyclerView。

我的目的是从第一个片段中的第二个片段中获取 EditText 的字体,以在列表中突出显示正确的字体。

我通过发送字体的哈希码成功地做到了这一点。但我觉得这不是一个好习惯。

我想你传入了一个带有字体名称的包,对吗?

请尝试输入字体名称,获取字体名称并设置

Intent intent=new Intent (..,..);
intent.putString("fontname","abc.ttf");
 startActivity(inent);

在存在编辑文本的第一个片段中,输入

String selectedEditTextFont = "Roboto-Medium.ttf";

mEditText.setFont(selectedEditTextFont);

public class ForexTextView extends AppCompatEditText { 

    public void setFont(String fontName) {
        Typeface myTypeface = Typeface.createFromAsset(
                getContext().getAssets(), "fonts/" + fontName);
        setTypeface(myTypeface);
    }
}

现在,selectedEditTextFont 字符串具有最终的 selectedFont 值。你需要通过 bundle 传递它。

 Bundle bundle = new Bundle();
 bundle.putString("SelectedFont", selectedEditTextFont);
 Fragment fragment = SecondFragment.newInstance();

在 recyclerview 所在的第二个片段中,您需要像这样在 adapter class.

中检查此 selectedEditTextFont
if(selectedEditTextFont.equals(arrRecyclerViewRow.get(ADAPTER_POSITION_HERE).getFontType){
//To highlight particular row
lLaylist.setBackGroundColor(getResources.getColor(R.color.yellow));
}

您需要使用关联列表制作 setFontType 并将其放入模型中 class。所以你可以在适配器 class.

中获取 getFontType()