在ListView中设置字体。马拉地语(kiran.ttk)

时间:2018-10-28 07:20:01

标签: android

enter image description here 帮助我在marathi(kiran.ttf)listview中设置字体。字体复制到res-font文件夹中。

1 个答案:

答案 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));