2017-03-31 57 views
0

由于documentation状态:无法解析法 '的getFont(?)'

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

结果

cannot resolve method 'getFont(?)' 

以前我曾经做到以下几点:

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

但现在,当我尝试创建字体内部资产文件夹文件夹,它会自动跳到里面资源文件夹。我正在使用android studio 2.3。预先感谢您的帮助。

+0

你是如何调用'getFont'?你有一个有效的'Resources'对象吗? – njzk2

+0

您是否定位到开发人员预览版? – Submersed

+0

@ njzk2我不知道。每当我尝试创建资产文件夹内的字体文件夹时,它会跳出它并进入res文件夹。 – Darush

回答

0

因为我没能文件夹直接从Android Studio中创建字体,我打开使用Windows资源管理器项目文件夹,并创建了字体app\src\main\assets\文件夹中粘贴我的字体文件存在。

正如很多其他人在回答中所建议的,我不得不使用Android Studio 2.4 Canary才能够使用Android O的新功能。该documentation状态:

Only Android Studio 2.4 includes support for all the new developer features available with Android O. So you need to get the canary version of Android Studio 2.4 to begin using the Android O SDK. But you can still keep your stable version of Android Studio installed.

0

尝试这样的事情

Typeface yourFont = Typeface.createFromAsset(getAssets(), "fonts/yourFont.ttf"); 
+0

谢谢你的回答,但我已经更新了这个问题。我曾经这样做,但我无法创建资产文件夹内的字体文件夹,因为它会自动创建res文件夹中的字体文件夹 – Darush

1

我没有用它了但是据我知道你可以指定Android O设备只有当你更新Android工作室2.4。而且您还需要将SDK更新到最新版本。因为医生说的getFont()在Android的O.

+0

感谢您的答案。我正在使用compileSdkVersion 25 buildToolsVersion“25.0.2”。很抱歉,SDK管理器目前没有更新可用。 – Darush

+1

让你的工作室从金丝雀通道下载更新,然后你可以下载studio 2..4 preview 3.之后,你可以下载SDK工具版本26.0.0 – Kunu

+0

谢谢,我目前正在下载新的更新,并会让每个人都知道结果。 – Darush

0

提供他们的方式我做它是这样的:

private TextView textView; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
. 
. 
. 
. 

textView = (TextView) findViewById(R.id.textView); 
Typeface mTextView = Typeface.createFromAsset(getAssets(),"fonts/your_font_style.ttf"); 
textView.setTypeface(mTextView); 

希望这会帮助你。

0

字体文件夹应位于资产文件夹下,因此请尝试在其中创建文件夹。

+0

字体文件夹自动跳出资产文件夹并进入res文件夹 – Darush

1

由于这是一种在O中发布的新方法,因此您必须将开发人员预览作为构建目标来使用它。欲了解更多信息,请查看link

0

我知道这可能是晚了,但这里的documentation使用与API版本下载的字体前26(兼容模式)。

基本上,你必须定义字体的描述如下所示(使用两个属性 - 应用程序和Android),然后使用ResourceCompat类以编程方式访问您的字体:

Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);

<?xml version="1.0" encoding="utf-8"?> 
<font-family xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
     <font android:fontStyle="normal" android:fontWeight="400" 
      android:font="@font/myfont-Regular" 
      app:fontStyle="normal" app:fontWeight="400" 
      app:font="@font/myfont-Regular"/> 
     <font android:fontStyle="italic" android:fontWeight="400" 
      android:font="@font/myfont-Italic" 
      app:fontStyle="italic" app:fontWeight="400" 
      app:font="@font/myfont-Italic" /> 
</font-family> 

干杯!