答案 0 :(得分:0)
欢迎来到
您可以使用此类来抓取视图组中的所有视图并更改其字体
public class FontChangeCrawler
{
private Typeface typeface;
public FontChangeCrawler(Typeface typeface)
{
this.typeface = typeface;
}
public FontChangeCrawler(AssetManager assets, String assetsFontFileName)
{
typeface = Typeface.createFromAsset(assets, assetsFontFileName);
}
public void replaceFonts(ViewGroup viewTree)
{
View child;
for(int i = 0; i < viewTree.getChildCount(); ++i)
{
child = viewTree.getChildAt(i);
if(child instanceof ViewGroup)
{
// recursive call
replaceFonts((ViewGroup)child);
}
else if(child instanceof TextView)
{
// base case
((TextView) child).setTypeface(typeface);
}
}
}
}
然后在列表视图适配器中使用getView
方法:
FontChangeCrawler fontChanger = new FontChangeCrawler(context.getAssets(), "fonts/IRANSansMobile(FaNum).ttf");
fontChanger.replaceFonts((ViewGroup)v);
以及活动中:
FontChangeCrawler fontChanger = new FontChangeCrawler(getAssets(), "fonts/IRANSansMobile(FaNum).ttf");
fontChanger.replaceFonts((ViewGroup)this.findViewById(android.R.id.content));