Android studioでTextViewのフォントを変更する2つの方法(例:模造歌、公式スクリプト)。
Androidのデフォルトフォントは3種類あり、TextViewにandroid:typeface="sans"を追加することで変更可能で、sansはそのうちの一つ、他の2つは" monospace" と" serif" で、3通りの使い方をしたくない場合は、外部フォントを使うことも可能です。
外部フォントを利用する方法はたくさんありますが、ここでは以下のような効果を持つ2つの方法を簡単に紹介します。
最初の方法 TextViewのフォントをコードで定義する。
ステップス
1. まず、図のようにsrc/mainファイルの下にassetsフォルダを作成し、その下に仕様のためにfontsフォルダを作成し、その下に弊社のフォントファイル(*.ttf)を配置することにします。
2. TextView コントロールをドラッグして、レイアウトファイルで準備します。
3. コード内でインポートしたフォントを見つけ、TextViewで使用します。
エフェクト
2つ目の方法:TextViewを継承したクラスを作成し、すべてをこのクラスに入れ、フォントを変更したい場所でこのクラスを使用します。
ステップス
1、新しいクラスを作成し、このクラスがTextViewを継承するようにします。
/**
* 1.version:
* 2.date:2017/1/13 15:26
* 3.update:2017/1/13.
* 4.autour:Zhang Yujie
*/
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util;
import android.widget;
TextView; /**
TextView; /** * Created by Administrator on 2015/10/27.
*/TextView; /** * Created by Administrator on 2015/10/27.
public class FontTextView extends TextView {
/* The file name of the font data in the assets directory */
private String mFontPath = null;
public FontTextView(Context context) {
super(context);
init(context, null, 0);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public FontTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs); init(context, attrs, 0); }
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
public String getFontPath() {
return mFontPath;
}
/**
*
Set font file fontPath
* @param fontPath The file name of the font data in the assets directory
*/
public void setFontPath(String fontPath) {
mFontPath = fontPath;
if (!TextUtils.isEmpty(mFontPath)) {
FontUtils.getInstance().replaceFontFromAsset(this, mFontPath);
}
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyleAttr, 0);
mFontPath = typedArray.getString(R.styleable.FontTextView_font_path);
typedArray.recycle();
if (!TextUtils.isEmpty(mFontPath)) {
FontUtils.getInstance().replaceFontFromAsset(this, mFontPath);
}
}
}
2. フォントを選択するクラスを作成する
/**
* 1.version:
* 2.date:2017/1/13 15:27
* 3.update:2017/1/13.
* 4.autour:ZhangYuJie
*/
import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.lang.ref.SoftReference;
import java.lang.ref;
import java.util.HashMap;
import java.util.Map;
public class FontUtils {
private static final String TAG = FontUtils.class.getSimpleName();
private Map<String, SoftReference<Typeface>> mCache = new HashMap<>();
private static FontUtils sSingleton = null;
public static Typeface DEFAULT = Typeface;
// disable instantiate
private FontUtils() {
}
public static FontUtils getInstance() {
// double check
if (sSingleton == null) {
synchronized (FontUtils.class) {
if (sSingleton == null) {
sSingleton = new FontUtils();
}
}
}
return sSingleton;
}
public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath) {
replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath));
}
public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath, int style) {
replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath), style);
}
public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath) {
replaceFont(root, createTypefaceFromFile(fontPath));
}
public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath, int style) {
replaceFont(root, createTypefaceFromFile(fontPath), style);
}
private void replaceFont(@NonNull View root, @NonNull Typeface typeface) {
if (root == null || typeface == null) {
return;
}
if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font
TextView textView = (TextView) root;
// Extract previous style of TextView
int style = Typeface.NORMAL;
if (textView.getTypeface() ! = null) {
style = textView.getTypeface().getStyle();
}
textView.setTypeface(typeface, style);
} else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); ++i) {
replaceFont(viewGroup.getChildAt(i), typeface);
}
} // else return
}
private void replaceFont(@NonNull View root, @NonNull Typeface typeface, int style) {
if (root == null || typeface == null) {
return;
}
if (style < 0 || style > 3) {
style = Typeface.NORMAL;
}
if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font
TextView textView = (TextView) root;
textView.setTypeface(typeface, style);
} else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); ++i) {
replaceFont(viewGroup.getChildAt(i), typeface, style);
}
} // else return
}
private Typeface createTypefaceFromAsset(Context context, String fontPath) {
SoftReference<Ty
3. レイアウトファイルの内部でクラスの完全なクラス名を直接使用し、ライブビューでその効果を確認できるようにします。
レンダリング
プロジェクトのアドレスです。
http://download.csdn.net/detail/lanrenxiaowen/9737027
関連
-
よくあるJavaのエラー50選(その1)
-
Androidアップグレード/ブラシ戦略(GoogleネイティブシステムPixel)
-
AndroidStdioのインストール時に「メインクラスが見つからない com/intelligi/idea/Main」というエラーが発生する。
-
Android Studioプロジェクトをインポートすると、無効なgradle jdkコンフィギュレーションが見つかったと報告される
-
エラー:SSLピアが正しくシャットダウンされない
-
エラーの解決方法 URIがandroid studioのxmlファイルに登録されていない。
-
テスト、Eclipseは "エラーが発生しました、詳細はエラーログを参照してください。 java.lang.NullPointerException" を報告します。
-
Android ビューの階層構造
-
AndroidManifest.xml ファイルが見つからない 解決策
-
APKのインストール: 失敗 [INSTALL_PARSE_FAILED_NO_CERTIFICATES]
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン