我想知道如何通过捆绑包放置字体?
字体似乎不可包装或序列化,因此无法捆绑
。我已经找到了用putInt()
和getInt()
传递字体哈希代码的提示,但我不知道它是否是一个干净的代码。
对不起,我的英语不好,谢谢你的帮助
编辑:
以下是一些详细信息,以了解我搜索要做什么:
我有两个片段:
- 首先,我有一个带有字体的编辑文本,我可以更改。
- 在第二个中,我有一个带有字体列表的回收器视图。
我的目的是从第二个片段中的第一个片段中获取 EditText 的字体,以突出显示列表中的正确字体。
我通过发送字体的哈希码成功地做到了这一点。但我不觉得这是一个好的做法。
在存在编辑文本的第一个片段中,将
字符串选定编辑文本字体 = "机器人媒体.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 = new Bundle();
bundle.putString("SelectedFont", selectedEditTextFont);
Fragment fragment = SecondFragment.newInstance();
在回收器视图所在的第二个片段中,您需要像在适配器类中一样检查此 selectedEditTextFont。
if(selectedEditTextFont.equals(arrRecyclerViewRow.get(ADAPTER_POSITION_HERE).getFontType){
//To highlight particular row
lLaylist.setBackGroundColor(getResources.getColor(R.color.yellow));
}
您需要使用关联的列表制作 setFontType,并需要将其放入模型类中。所以你可以在适配器类中获取FontType()。
我认为你传递了一个带有字体名称的捆绑包,对吗?
请尝试字体的传递名称并获取字体名称和设置。
Intent intent=new Intent (..,..);
intent.putString("fontname","abc.ttf");
startActivity(inent);