我有一个带有启动画面的应用程序,然后是带有一些 TextViews 的主要活动。
我正在使用我从 Attributes > fontFamily > More Fonts... .
问题是,当我启动我的应用程序时,启动画面正确显示但是当我的主要活动跟随时,您可以看到我的 TextView 中的字体从“默认”跳到 aladin 的一毫秒。
换句话说,似乎 aladin 字体渲染/加载花费了太多时间。
我该如何解决这个问题??
我已经尝试过将文本大小从 sp 更改为 dp,但这并不能解决我的问题。
aladin 字体应该立即出现,而不是在一毫秒之后,它看起来不太好。
非常感谢您。
<TextView
android:id="@+id/textViewItem"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:fontFamily="@font/aladin"
android:gravity="center|center_horizontal"
android:textColor="#000000"
android:textSize="26sp" />
编辑: 这是我的 AndroidManifest.xml 的一部分:
<activity
android:name=".SplashActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustPan"
android:label="@string/app_name">
这是我的 SplashActivity.java 的一部分:
public class SplashActivity extends AppCompatActivity {
private Handler mHandler;
private Runnable mRunnable;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mRunnable = new Runnable() {
@Override
public void run() {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
};
mHandler=new Handler();
mHandler.postDelayed(mRunnable, 1500);
}
@Override
protected void onDestroy (){
super.onDestroy();
if(mHandler!=null && mRunnable !=null)
mHandler.removeCallbacks(mRunnable);
}
}
我有类似的问题;当应用程序启动时,文本会以默认字体显示几分之一秒,然后以自定义字体重新绘制。这可能不会打扰大多数用户,但它绝对引人注目。
我还发现,一旦加载了自定义字体,随后的文本将在不先显示默认字体的情况下绘制。
所以,我的解决方法是在显示初始屏幕时加载自定义字体。首先,准备自定义字体。
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="Ubuntu Mono">
</font-family>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="preloaded_fonts" translatable="false">
<item>@font/ubuntu_mono</item>
</array>
</resources>
...
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
然后在初始屏幕的布局中,添加一个没有文本的 TextView。这将加载自定义字体,当主字体出现时,自定义字体将准备就绪。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
app:fontFamily="@font/ubuntu_mono" <-- custom font
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
有些字体很大,所以加载需要一些时间,即几毫秒, 有一个简单的解决方案,即通过创建 Typeface 类的实例,在字体出现在 UI 之前以编程方式加载字体: 例如,如果您要使用名为“alegreya”的特定字体,请执行以下操作:
//First you need to initialize and hook your textview
TextView textView;
textView = findViewById(R.id.myTxtView);
//initialize the font from ressources
Typeface typeFace = ResourcesCompat.getFont(getApplicationContext(),R.font.alegreya);
//set the font to the textview
textView.setTypeface(typeFace);
现在您可以避免从文件加载自定义字体时发生的延迟。这是因为字体是由字体提供者下载和缓存的,所以在需要的时候可以更快地访问它。
希望这已经解决了您的问题:)