Adding a custom android font with lib

I use a custom font android lib Calligraphy https://github.com/chrisjenx/Calligraphy .

But there is no effect on the text image. I am using the code below:

    public class MainActivity extends Activity {

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

    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(new CalligraphyContextWrapper(newBase));
    }
}

In XML:

<TextView
        tools:context=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Touch Listner"
        android:textSize="18sp"
        app:fontPath="fonts/gotham-book.ttf" />

At attrs:

<resources>
    <attr name="fontPath" format="string"/>
</resources>

In assets / font / gotham-book.ttf

+4
source share
7 answers

Create a class that extends the application, now the following code in it. and declare it in the manifest under the application tag.

    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                    .setDefaultFontPath("fonts/Roboto-ThinItalic.ttf")
                    .setFontAttrId(R.attr.fontPath)
                    .build()
    );

+3
source

Remove the namespace from TextView.

<TextView
    tools:context=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Touch Listner"
    android:textSize="18sp"
    fontPath="fonts/gotham-book.ttf" />

(Change app:fontPathto fontPath)

It is annoying that we cannot resolve the namespace auto-replace.

+3

, Application. - :

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/DINNextLTPro-Regular.otf")
                .setFontAttrId(R.attr.fontPath)
                .build()
        );
    }

}

, , AndroidManifest.xml MyApplication android:name , :

<?xml version="1.0" encoding="utf-8"?>
<manifest package="it.sbsmobile.golife"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name="com.example.MyApplication">
        <activity
            android:name=".Profile1"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

, https://github.com/chrisjenx/Calligraphy , , activity :

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

: LINK

Application?

+3

1) (yourfont.ttf)

2) /

 Typeface yourfont = Typeface.createFromAsset(getAssets(),
            "fonts/yourfont.ttf");

3) TextView ( , ) setTypeface . .

TextView textview1 = (TextView) findViewById(R.id.exampletextview);
textview1.setTypeface(yourfont);

, , , , , , Typeface . , Textview. - :

public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyTextView(Context context) {
    super(context);
    init();
}

public void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/yourfont.ttf");
    setTypeface(tf ,1);

}

}

xml <com.yourpackagename.MyTextView />

+1

EasyFonts, TextView . , assets/fonts. Typeface.

. .

  • Roboto
  • Droid Serif
  • Droid Robot
  • Fun Raiser
  • Android Nation

:

TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
0

.

.

-, xml ( )

xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingPrefix"

, Activity,

protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

Yooooo, !

, , http://i.stack.imgur.com/F87JE.png

0

All Articles