でフォントを変更するにはどうすればよいTextView
ですか? デフォルトでは Arial として表示されますか? に変更するにはHelvetica
?
16 に答える
まず、デフォルトは Arial ではありません。デフォルトは Droid Sans です。
次に、別の組み込みフォントに変更するにはandroid:typeface
、レイアウト XML またはsetTypeface()
Java で使用します。
第 3 に、Android には Helvetica フォントがありません。組み込みの選択肢は、Droid Sans ( sans
)、Droid Sans Mono ( monospace
)、および Droid Serif ( serif
) です。独自のフォントをアプリケーションにバンドルして 経由setTypeface()
で使用できますが、フォント ファイルはサイズが大きく、場合によってはライセンス契約が必要になることに注意してください (例: Helvetica、Linotype フォント)。
編集
Android のデザイン言語は、スケール、スペース、リズム、基になるグリッドとの位置合わせなど、従来のタイポグラフィ ツールに依存しています。ユーザーが情報画面をすばやく理解できるようにするには、これらのツールを適切に展開することが不可欠です。このようなタイポグラフィの使用をサポートするために、Ice Cream Sandwich は Roboto という名前の新しいタイプ ファミリを導入しました。これは、UI と高解像度画面の要件に合わせて特別に作成されたものです。
現在の TextView フレームワークは、薄い、軽い、通常、および太字のウェイトで Roboto を提供し、各ウェイトにはイタリック スタイルを使用します。このフレームワークは、Roboto Condensed バリアントも通常の太字の太字で提供し、各太さのイタリック スタイルも提供します。
ICS の後、Android には Roboto フォント スタイルが含まれています。
編集2
サポート ライブラリ 26 の登場により、Android はデフォルトでカスタム フォントをサポートするようになりました。XML またはプログラムで TextViews に個別に設定できるres/fontsに新しいフォントを挿入できます。アプリケーション全体のデフォルト フォントは、styles.xml を定義することで変更することもできます
まず、.ttf
必要なフォントのファイルをダウンロードします ( arial.ttf
)。フォルダに入れ assets
ます。(assets フォルダー内にfontsという名前の新しいフォルダーを作成し、その中に配置します。) 次のコードを使用して、フォントを に適用しますTextView
。
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/arial.ttf");
textView.setTypeface(type);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/DroidSansFallback.ttf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);
すべてのフォントを含む静的クラスを作成することをお勧めします。そうすれば、パフォーマンスに悪影響を与える可能性のあるフォントを複数回作成することはありません。「 assets 」フォルダの下に「fonts 」というサブフォルダを作成してください。
次のようなことをします:
public class CustomFontsLoader {
public static final int FONT_NAME_1 = 0;
public static final int FONT_NAME_2 = 1;
public static final int FONT_NAME_3 = 2;
private static final int NUM_OF_CUSTOM_FONTS = 3;
private static boolean fontsLoaded = false;
private static Typeface[] fonts = new Typeface[3];
private static String[] fontPath = {
"fonts/FONT_NAME_1.ttf",
"fonts/FONT_NAME_2.ttf",
"fonts/FONT_NAME_3.ttf"
};
/**
* Returns a loaded custom font based on it's identifier.
*
* @param context - the current context
* @param fontIdentifier = the identifier of the requested font
*
* @return Typeface object of the requested font.
*/
public static Typeface getTypeface(Context context, int fontIdentifier) {
if (!fontsLoaded) {
loadFonts(context);
}
return fonts[fontIdentifier];
}
private static void loadFonts(Context context) {
for (int i = 0; i < NUM_OF_CUSTOM_FONTS; i++) {
fonts[i] = Typeface.createFromAsset(context.getAssets(), fontPath[i]);
}
fontsLoaded = true;
}
}
このようにして、アプリケーションのどこからでもフォントを取得できます。
上記の答えは正しいです。そのコードを使用している場合は、「assets」フォルダーの下に「fonts」というサブフォルダーを必ず作成してください。
フォント作成を統合する別の方法...
public class Font {
public static final Font PROXIMA_NOVA = new Font("ProximaNovaRegular.otf");
public static final Font FRANKLIN_GOTHIC = new Font("FranklinGothicURWBoo.ttf");
private final String assetName;
private volatile Typeface typeface;
private Font(String assetName) {
this.assetName = assetName;
}
public void apply(Context context, TextView textView) {
if (typeface == null) {
synchronized (this) {
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), assetName);
}
}
}
textView.setTypeface(typeface);
}
}
そして、あなたの活動に使用するには...
myTextView = (TextView) findViewById(R.id.myTextView);
Font.PROXIMA_NOVA.apply(this, myTextView);
この volatile フィールドを使用した二重チェックのロック イディオムは、Java 1.5+ で使用されるメモリ モデルでのみ正しく機能します。
import java.lang.ref.WeakReference;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Typeface;
public class FontsManager {
private static FontsManager instance;
private static HashMap<String, WeakReference<Typeface>> typefaces = new HashMap<String, WeakReference<Typeface>>();
private static Context context;
private FontsManager(final Context ctx) {
if (context == null) {
context = ctx;
}
}
public static FontsManager getInstance(final Context appContext) {
if (instance == null) {
instance = new FontsManager(appContext);
}
return instance;
}
public static FontsManager getInstance() {
if (instance == null) {
throw new RuntimeException(
"Call getInstance(Context context) at least once to init the singleton properly");
}
return instance;
}
public Typeface getFont(final String assetName) {
final WeakReference<Typeface> tfReference = typefaces.get(assetName);
if (tfReference == null || tfReference.get() == null) {
final Typeface tf = Typeface.createFromAsset(context.getResources().getAssets(),
assetName);
typefaces.put(assetName, new WeakReference<Typeface>(tf));
return tf;
}
return tfReference.get();
}
}
このようにして、TextView から継承し、コンストラクターで setTypeface を呼び出すビューを作成できます。
多分もう少し単純なもの:
public class Fonts {
public static HashSet<String,Typeface> fonts = new HashSet<>();
public static Typeface get(Context context, String file) {
if (! fonts.contains(file)) {
synchronized (this) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), name);
fonts.put(name, typeface);
}
}
return fonts.get(file);
}
}
// Usage
Typeface myFont = Fonts.get("arial.ttf");
(このコードはテストされていませんが、一般的にこのアプローチはうまく機能するはずです。)