AndroidアプリでTextViewに使用できるフォントの一覧

Androidアプリでフォントをアプリ側で変更するには、フォントファミリーの指定またはフォントスタイルの指定となります。

TextViewに独自フォントを使用する方法

フォントファミリーの種類は
DEFAULT
DEFAULT_BOLD
SANS_SERIF
SERIF
MONOSPACE

フォントスタイルの種類は
NORMAL
BOLD
ITALIC
BOLD_ITALIC

指定の方法は次のようになります。

textView.setTypeface(【フォントファミリー】);
textView.setTypeface(Typeface.SANS_SERIF);

または

textView.setTypeface(Typeface.create(【フォントファミリー】, 【フォントスタイル】));
textView.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC));

横長に一覧を表示するサンプル
縦長に一覧を表示するサンプル

端末の設定でフォントを変更していた場合はこの表示にはなりません。

横長に一覧を表示するサンプル

package com.example.sample.text;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TextSampleAppActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ScrollView mainView = new ScrollView(this);
		setContentView(mainView);

		TableLayout tableLayout = new TableLayout(this);
		mainView.addView(tableLayout);
		
		TableRow tablerow;
		TextView txtSample;
		int i,j,k;
		String[] sampleTextStrings = {"abcd", "0123", "あいうえお", "アイウエオ", "アイウエオ", "日本語"};

		String[] typeface1Names = {"指定なし", "DEFAULT", "DEFAULT_BOLD", "SANS_SERIF", "SERIF", "MONOSPACE"};
		Typeface[] typefaces1 = {
				null, Typeface.DEFAULT, Typeface.DEFAULT_BOLD, Typeface.SANS_SERIF, Typeface.SERIF, Typeface.MONOSPACE
				};

		String[] typeface2Names = {"", "NORMAL", "BOLD", "ITALIC", "BOLD_ITALIC"};
		Integer[] typefaces2 = {
				null, Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC
				};
		

		for(k = 0 ; k < typefaces2.length ; k++){
			for(j = 0 ; j < typefaces1.length ; j++){
				tablerow = new TableRow(this);
				tableLayout.addView(tablerow);
				
				txtSample = new TextView(this);

				if(typefaces2[k] == null){
					txtSample.setText(typeface1Names[j]);
				}else{
					txtSample.setText(typeface1Names[j] + " " + typeface2Names[k]);
				}

				if(typefaces1[j] == null){
					
				}else if(typefaces2[k] == null){
					txtSample.setTypeface(typefaces1[j]);
				}else{
					txtSample.setTypeface(Typeface.create(typefaces1[j], typefaces2[k]));
				}
				tablerow.addView(txtSample);
				
				txtSample = new TextView(this);
				txtSample.setText(" : ");
				tablerow.addView(txtSample);
				
				for(i = 0 ; i < sampleTextStrings.length ; i++){
					txtSample = new TextView(this);
					txtSample.setText(sampleTextStrings[i]);
					if(typefaces1[j] == null){
						
					}else if(typefaces2[k] == null){
						txtSample.setTypeface(typefaces1[j]);
					}else{
						txtSample.setTypeface(Typeface.create(typefaces1[j], typefaces2[k]));
					}
					tablerow.addView(txtSample);
				}
			}
		}
	}
}

縦長に一覧を表示するサンプル

package com.example.sample.text;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TextSampleAppActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ScrollView mainView = new ScrollView(this);
		setContentView(mainView);

		TableLayout tableLayout = new TableLayout(this);
		mainView.addView(tableLayout);
		
		TableRow tablerow;
		TextView txtSample;
		TableRow.LayoutParams rowLayout;
		int i,j,k;
		String[] sampleTextStrings = {"abcd", "0123", "あいうえお", "アイウエオ", "アイウエオ", "日本語"};

		String[] typeface1Names = {"指定なし", "DEFAULT", "DEFAULT_BOLD", "SANS_SERIF", "SERIF", "MONOSPACE"};
		Typeface[] typefaces1 = {
				null, Typeface.DEFAULT, Typeface.DEFAULT_BOLD, Typeface.SANS_SERIF, Typeface.SERIF, Typeface.MONOSPACE
				};

		String[] typeface2Names = {"", "NORMAL", "BOLD", "ITALIC", "BOLD_ITALIC"};
		Integer[] typefaces2 = {
				null, Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC
				};

		for(k = 0 ; k < typefaces2.length ; k++){
			for(j = 0 ; j < typefaces1.length ; j++){
				tablerow = new TableRow(this);
				tableLayout.addView(tablerow);
				
				txtSample = new TextView(this);

				if(typefaces2[k] == null){
					txtSample.setText(typeface1Names[j]);
				}else{
					txtSample.setText(typeface1Names[j] + " " + typeface2Names[k]);
				}

				if(typefaces1[j] == null){
					
				}else if(typefaces2[k] == null){
					txtSample.setTypeface(typefaces1[j]);
				}else{
					txtSample.setTypeface(Typeface.create(typefaces1[j], typefaces2[k]));
				}
				rowLayout = new TableRow.LayoutParams();
				rowLayout.span = sampleTextStrings.length;

				tablerow.addView(txtSample,rowLayout);
				
				tablerow = new TableRow(this);
				tablerow.setPadding(0,0,0,10);
				tableLayout.addView(tablerow);
				
				for(i = 0 ; i < sampleTextStrings.length ; i++){
					txtSample = new TextView(this);
					txtSample.setText(sampleTextStrings[i]);
					if(typefaces1[j] == null){
						
					}else if(typefaces2[k] == null){
						txtSample.setTypeface(typefaces1[j]);
					}else{
						txtSample.setTypeface(Typeface.create(typefaces1[j], typefaces2[k]));
					}
					tablerow.addView(txtSample);
				}
			}
		}
	}
}

関連記事

スポンサーリンク

background-position-y 背景画像の縦位置を指定する

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る