我不想使用我的应用程序的默认字体。
我想使用我想要的字体。
因此,我将所需的字体复制到 assets/fonts/
文件夹中,并按如下所示进行编码。
但是,出现错误。
请帮助我在完整的应用程序中使用相同的字体。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundResource(R.drawable.bg);
this.setContentView(ll);
TextView tv = (TextView) findViewById(R.id.app_name);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/TAU_MADN.TTF");
tv.setTypeface(face);
更新:
我的 xml TextView
代码;
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="app_name"
android:textStyle="bold" />
错误:
error: Error: String types not allowed (at 'id' with value 'app_name').
最佳答案
您的错误与将自定义字体作为资源加载无关。
在您的布局R.layout.activity_main
中,没有id为R.id.app_name
的TextView
,因此当您初始化它时在您的代码中的 TextView
将会抛出错误。
确保您的布局中有这样的TextView
。
如果您认为布局中已经确实有该 TextView
,请尝试清理该项目。
编辑
添加如下id:
android:id="@+id/app_name"
还要使用小写的“.ttf”扩展名重命名您的字体,并且对它的引用都相同。
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/TAU_MADN.ttf"); // lowercased ".ttf" reference
参见here有关您的问题的更多信息。
关于java - android:整个应用程序的字体系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17986667/