gpt4 book ai didi

java - 将自定义字体添加到 Android 应用程序

转载 作者:行者123 更新时间:2023-11-30 09:15:04 26 4
gpt4 key购买 nike

我正在尝试将自定义字体添加到我的 android 应用程序中。这是我的代码。谁能告诉我哪里错了?

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView textview = (TextView) findViewById(R.id.FontTest);

Typeface tf = Typeface.createFromAsset(getApplicationContext()
.getAssets(), "fonts/snap-itc.ttf");
textview.setTypeface(tf);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/FontTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Font Test"
android:textSize="50px"/>
</RelativeLayout>

最佳答案

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/snap-itc.ttf");

代替:

Typeface tf = Typeface.createFromAsset(getApplicationContext()
.getAssets(), "fonts/snap-itc.ttf");

关于java - 将自定义字体添加到 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20095146/

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