ExpandableList中的外部字体

时间:2017-01-27 09:46:19

标签: java android android-typeface

我正在尝试在我的一个可扩展列表视图中设置外部字体。我正在尝试如下

public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    String headerTitleDate = (String) getGroupD(groupPosition);

    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_group, null);

    }

    Typeface bold = Typeface.createFromAsset(getContext().getAssets(), "shruti.ttf");
    TextView listTitle = (TextView) convertView
            .findViewById(R.id.tv_listtitle);
    TextView listTitleDate = (TextView) convertView
            .findViewById(R.id.tv_date);
    listTitle.setTypeface(null, Typeface.BOLD);

    listTitle.setText(headerTitle);

    listTitleDate.setTypeface(null, Typeface.BOLD);

    listTitleDate.setText(headerTitleDate);

    return convertView;
}

但是我得到的getAssets()无法解决。我试图将它与Context一起使用,但没有它但没有成功。任何人都可以告诉我它有什么问题吗?

由于

2 个答案:

答案 0 :(得分:0)

在执行此操作时,您应该使用视图的上下文,因为它位于适配器内。

您可以通过以下方式获取视图的上下文:view.getContext()

取代这一行:

Typeface bold = Typeface.createFromAsset(getContext().getAssets(), "shruti.ttf");

用这个:

Typeface bold = Typeface.createFromAsset(convertView.getContext().getAssets(), "shruti.ttf");

将修复错误。希望这有帮助,如果有,请告诉我。

答案 1 :(得分:0)

我就是这样做的

第1步:

我首先制作customizable字体类,对于您使用shruti.ttf

的情况
    public class Shruti  extends TextView{




    public Champagne(Context context) {
        super(context);

        applyCustomFont(context);
    }

    public Champagne(Context context, AttributeSet attrs) {
        super(context, attrs);

        applyCustomFont(context);
    }

    public Champagne(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        applyCustomFont(context);
    }

    private void applyCustomFont(Context context) {
        Typeface customFont = FontCache.getTypeface("shruti.ttf", context);
        setTypeface(customFont);
    }

}

第2步:

然后你还包括一个FontCache

public class FontCache {


    private static HashMap<String, Typeface> fontCache = new HashMap<>();

    public static Typeface getTypeface(String fontname, Context context) {
        Typeface typeface = fontCache.get(fontname);

        if (typeface == null) {
            try {
                typeface = Typeface.createFromAsset(context.getAssets(), fontname);
            } catch (Exception e) {
                return null;
            }

            fontCache.put(fontname, typeface);
        }

        return typeface;
    }
}

第3步:

然后,您将自定义课程附加到TextView。在您的xml项目类中,如此

<company.override.huzykamz.pixsar.customization.Shruti
            android:layout_width="match_parent"
            android:id="@+id/post_desc"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#000"
            android:textSize="15dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingBottom="15dp" />

注:

company.override.huzykamz.pixsar.customization is just a package name example , but you just put yours.

而不是

<TextView
            android:layout_width="match_parent"
            android:id="@+id/post_desc"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#000"
            android:textSize="15dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingBottom="15dp" />

希望它运作良好。 这是一个确定的交易解决方案