Android-custom-fonts
Android-カスタムフォント
Androidでは、アプリケーションの文字列に独自のカスタムフォントを定義できます。 必要なフォントをインターネットからダウンロードして、それをassets/fontsフォルダに配置するだけです。
fontsフォルダーの下のassetフォルダーにフォントを配置した後、Typefaceクラスを介してJavaコードでフォントにアクセスできます。 最初に、コード内のテキストビューの参照を取得します。 その構文は以下のとおりです-
TextView tx = (TextView)findViewById(R.id.textview1);
次に行う必要があるのは、Typefaceクラスの静的メソッド* createFromAsset()*を呼び出して、アセットからカスタムフォントを取得することです。 その構文は以下のとおりです-
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
最後に行う必要があるのは、このカスタムフォントオブジェクトをTextView Typefaceプロパティに設定することです。 そのためには、* setTypeface()*メソッドを呼び出す必要があります。 その構文は以下のとおりです-
tx.setTypeface(custom_font);
これらのメソッドとは別に、フォントをより効果的に処理するために使用できるTypefaceクラスで定義された他のメソッドがあります。
Sr.No | Method & description |
---|---|
1 |
create(String familyName, int style) ファミリ名とオプションスタイル情報を指定してTypefaceオブジェクトを作成します |
2 |
create(Typeface family, int style) 指定された既存の書体と指定されたスタイルに最も一致する書体オブジェクトを作成します |
3 |
createFromFile(String path) 指定したフォントファイルから新しい書体を作成する |
4 |
defaultFromStyle(int style) 指定されたスタイルに基づいて、デフォルトのTypefaceオブジェクトの1つを返します |
5 |
getStyle() タイプフェイスの固有のスタイル属性を返します |
例
以下に、TypeFontを使用してCustomFontを処理する例を示します。 フォントファイルで指定したカスタムフォントを表示する基本的なアプリケーションを作成します。
この例を試すには、実際のデバイスまたはエミュレーターでこれを実行できます。
Steps | Description |
---|---|
1 | You will use Android studio IDE to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Download a font from internet and put it under assets/fonts folder. |
3 | Modify src/MainActivity.java file to add necessary code. |
4 | Modify the res/layout/activity_main to add respective XML components |
5 | Run the application and choose a running android device and install the application on it and verify the results |
コードパーツに入る前に、Windowsエクスプローラーからassestsフォルダーにフォントを追加します。
Android Loading Spinner Tutorial
以下は、変更されたメインアクティビティファイル MainActivity.java の内容です。
package com.example.sairamkrishna.myapplication;
import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView tv1,tv2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView3);
tv2=(TextView)findViewById(R.id.textView4);
Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf");
tv1.setTypeface(face);
Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
tv2.setTypeface(face1);
}
}
以下は、xml activity_main.xml の変更されたコンテンツです。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Typeface"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView3"
android:layout_centerVertical="true"
android:textSize="45dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView4"
android:layout_below="@+id/textView3"
android:layout_alignLeft="@+id/textView3"
android:layout_alignStart="@+id/textView3"
android:layout_marginTop="73dp"
android:textSize="45dp"/>
</RelativeLayout>
以下は res/values/string.xml の内容です。
<resources>
<string name="app_name">My Application</string>
</resources>
以下は AndroidManifest.xml ファイルの内容です。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
変更したばかりのカスタムフォントアプリケーションを実行してみましょう。 環境設定中に AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開いて、ツールバーのRun Eclipse Run Iconアイコンをクリックします。AndroidStudioはAVDにアプリをインストールして起動し、すべてがセットアップとアプリケーションで問題ない場合、次のエミュレータウィンドウが表示されます-
Android Loading Spinner Tutorial
ご覧のとおり、AVDに表示されたテキストにはデフォルトのAndroidフォントはなく、fontsフォルダーで指定したカスタムフォントが含まれています。
注-カスタムフォントを使用する場合は、フォントでサポートされているサイズと文字に注意する必要があります。