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

Java 自定义字体不适用于Android 5.0,但适用于其他地方,java,android,xml,android-5.0-lollipop,android-custom-view,Java,Android,Xml,Android 5.0 Lollipop,Android Custom View,我在我的应用程序中使用自定义字体,问题是,它在android版本上的效果比棒棒糖小,比棒棒糖大,但在棒棒糖上就变成空白了 请查找附加的代码和屏幕截图 SelectionPage.java package com.execube.volantis.views; import android.content.Intent; import android.graphics.Color; import android.graphics.Typeface; import android.net.Uri;

我在我的应用程序中使用自定义字体,问题是,它在android版本上的效果比棒棒糖小,比棒棒糖大,但在棒棒糖上就变成空白了

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

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);
            }
        });

    }
}
按钮使用自定义字体

只有当android版本不等于棒棒糖时,有没有办法设置自定义字体?

这是棉花糖API 23

这是棒棒糖API 21


在应用程序类中定义类型面

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安卓5.0使用默认主题

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

单空间
可能对您有所帮助