gpt4 book ai didi

java - 无法解析方法 'getFont(?)'

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:21:38 24 4
gpt4 key购买 nike

作为documentation状态:

Android O lets you bundle fonts as resources by adding the font file in the res/font/ folder.

结果:

You can retrieve fonts by using the getFont(int) method, where you need to pass the resource identifier of the font that you want to retrieve. This method returns a Typeface object. This encodes the first of the weight or style variants of your font, if it is a font family. You can then use the Typeface.create(typeface, style) method to retrieve specific styles.

Note: The TextView already does this for you.

Typeface typeface = getResources().getFont(R.font.myfont); textView.setTypeface(typeface);

不幸的是,当我使用上面的代码时,出现以下错误:

cannot resolve method 'getFont(?)'

以前我曾经做过以下事情:

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");

但是现在,当我尝试在 assets 文件夹中创建 fonts 文件夹时,它会自动跳转到 res 文件夹中。

我正在使用 android studio 2.3。在此先感谢您的帮助。

最佳答案

使用支持库 26 中的此方法。

ResourcesCompat.getFont(context, R.font.your_font);

来源: https://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html

关于java - 无法解析方法 'getFont(?)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43142235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com