自定义字体无法在Android 5.0上运行,但可在其他地方使用

时间:2016-04-16 02:16:59

标签: java android xml android-5.0-lollipop android-custom-view

我在我的应用程序中使用自定义字体,问题是,它适用于Android版本少于Lollipop并且大于lolliop但是在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);
            }
        });

    }
}

按钮使用自定义字体。

如果Android版本不等于Lollipop,有没有办法设置自定义字体?

这是Marshmallow API 23

**Here's Marshmallow API 23**

这是Lollipop API 21

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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>