gpt4 book ai didi

java - 自定义字体不适用于 Android 5.0,但适用于其他地方

转载 作者:行者123 更新时间:2023-12-05 07:49:59 24 4
gpt4 key购买 nike

我在我的应用程序中使用自定义字体,问题是,它适用于低于 Lollipop 和高于 Lollipop 的 Android 版本,但在 Lollipop 上它只是空白

请查找附件中的代码和屏幕截图

SelectionPage.java

package com.execube.volantis.views;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.crashlytics.android.Crashlytics;
import com.execube.volantis.R;
import com.execube.volantis.receiver.customHandler;
import com.flaviofaria.kenburnsview.KenBurnsView;
import com.flaviofaria.kenburnsview.RandomTransitionGenerator;
import com.pushbots.push.Pushbots;

import io.fabric.sdk.android.Fabric;

public class SelectionPage extends AppCompatActivity {

private Button mEventsButton;
private Button mAlertsButton;
private Button mAboutButton;
private Button mDirectionsButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.selection_page);

Pushbots.sharedInstance().init(this);

Pushbots.sharedInstance().setCustomHandler(customHandler.class);











mEventsButton=(Button)findViewById(R.id.events_button);
mAboutButton=(Button)findViewById(R.id.about_button);
mAlertsButton=(Button)findViewById(R.id.alerts_button);
mDirectionsButton=(Button)findViewById(R.id.directions_button);




Typeface gothamRoundedMedium= Typeface.createFromAsset(getAssets(),"fonts/Gotham-Rounded-Medium.ttf");
mEventsButton.setTypeface(gothamRoundedMedium);
mAboutButton.setTypeface(gothamRoundedMedium);
mDirectionsButton.setTypeface(gothamRoundedMedium);
mAlertsButton.setTypeface(gothamRoundedMedium);





mEventsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(SelectionPage.this,EventActivity.class);

startActivity(intent);
}
});


mDirectionsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String geoUri = "http://maps.google.com/maps?q=loc:" + "12.9021902" + "," + "77.518582" + " (" + "RNS Institute Of Technlogy" + ")";
Intent intent= new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
startActivity(intent);
}
});

mAlertsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(SelectionPage.this,AlertsActivity.class);
startActivity(intent);
}
});

mAboutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(SelectionPage.this,AboutActivity.class);
startActivity(intent);
}
});

}
}

按钮使用自定义字体。

有什么办法可以设置自定义字体只有当安卓版本不等于 Lollipop ?

这是 Marshmallow API 23

**Here's Marshmallow API 23**

这是 Lollipop API 21

enter image description here

最佳答案

在应用类中定义字体。

public final class FontsOverride {

public static void setDefaultFont(Context context,
String staticTypefaceFieldName, String fontAssetName) {
final Typeface regular = Typeface.createFromAsset(context.getAssets(),
fontAssetName);
replaceFont(staticTypefaceFieldName, regular);
}

protected static void replaceFont(String staticTypefaceFieldName,
final Typeface newTypeface) {
try {
final Field staticField = Typeface.class
.getDeclaredField(staticTypefaceFieldName);
staticField.setAccessible(true);
staticField.set(null, newTypeface);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

然后您需要重载一些默认字体,例如在应用程序类中:

public final class Application extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");
}
}

API 21 Android 5.0 使用默认主题

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:typeface">monospace</item>
</style>

关于java - 自定义字体不适用于 Android 5.0,但适用于其他地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36659451/

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