編集:それでしばらく経ちました、そして私はこれをするための最良の方法であると私が思うものを追加したいと思います、そしてXMLを通してそれ以上です!
したがって、最初に、カスタマイズするビューをオーバーライドする新しいクラスを作成する必要があります。(たとえば、カスタム書体のボタンが必要ですか?拡張Button
)。例を見てみましょう:
public class CustomButton extends Button {
private final static int ROBOTO = 0;
private final static int ROBOTO_CONDENSED = 1;
public CustomButton(Context context) {
super(context);
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
parseAttributes(context, attrs); //I'll explain this method later
}
public CustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
parseAttributes(context, attrs);
}
}
ここで、お持ちでない場合は、の下res/values/attrs.xml
にXMLドキュメントを追加し、次を追加します。
<resources>
<!-- Define the values for the attribute -->
<attr name="typeface" format="enum">
<enum name="roboto" value="0"/>
<enum name="robotoCondensed" value="1"/>
</attr>
<!-- Tell Android that the class "CustomButton" can be styled,
and which attributes it supports -->
<declare-styleable name="CustomButton">
<attr name="typeface"/>
</declare-styleable>
</resources>
さて、それで邪魔にならないように、parseAttributes()
前のメソッドに戻りましょう:
private void parseAttributes(Context context, AttributeSet attrs) {
TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.CustomButton);
//The value 0 is a default, but shouldn't ever be used since the attr is an enum
int typeface = values.getInt(R.styleable.CustomButton_typeface, 0);
switch(typeface) {
case ROBOTO: default:
//You can instantiate your typeface anywhere, I would suggest as a
//singleton somewhere to avoid unnecessary copies
setTypeface(roboto);
break;
case ROBOTO_CONDENSED:
setTypeface(robotoCondensed);
break;
}
values.recycle();
}
これですべての設定が完了しました。ほぼすべての属性を追加できます(typefaceStyleに別の属性を追加できます-太字、斜体など)が、それを使用する方法を見てみましょう。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.yourpackage.name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.yourpackage.name.CustomButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
custom:typeface="roboto" />
</LinearLayout>
線は実際にはxmlns:custom
何でもかまいませんが、規則は上に示したものです。重要なのは、それが一意であるということです。そのため、パッケージ名が使用されます。これcustom:
で、属性にプレフィックスを使用し、 android:
Android属性にプレフィックスを使用します。
最後にもう1つ、これをスタイル(res/values/styles.xml
)で使用する場合は、行を追加しないxmlns:custom
でください。プレフィックスなしで属性の名前を参照するだけです。
<style name="MyStyle>
<item name="typeface">roboto</item>
</style>
(PREVIOUS ANSWER)
Androidでカスタム書体を使用する
これは役立つはずです。基本的に、これをXMLで行う方法はありません。私が知る限り、コードでこれを行う簡単な方法はありません。書体を一度作成してから、それぞれに対してsetTypeface()を実行するsetLayoutFont()メソッドをいつでも使用できます。レイアウトに新しいアイテムを追加するたびに更新する必要があります。以下のようなもの:
public void setLayoutFont() {
Typeface tf = Typeface.createFromAsset(
getBaseContext().getAssets(), "fonts/BPreplay.otf");
TextView tv1 = (TextView)findViewById(R.id.tv1);
tv1.setTypeface(tf);
TextView tv2 = (TextView)findViewById(R.id.tv2);
tv2.setTypeface(tf);
TextView tv3 = (TextView)findViewById(R.id.tv3);
tv3.setTypeface(tf);
}
編集:それで私はちょうどこのようなものを自分で実装することに取り掛かりました、そして私がそれをやった方法はこのような関数を作ることでした:
public static void setLayoutFont(Typeface tf, TextView...params) {
for (TextView tv : params) {
tv.setTypeface(tf);
}
}
次に、onCreate()からこのメソッドを使用して、更新するすべてのTextViewを渡します。
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/BPreplay.otf");
//find views by id...
setLayoutFont(tf, tv1, tv2, tv3, tv4, tv5);
9/5/12編集:
それで、これはまだ意見と投票を得ているので、私ははるかに良くてより完全な方法を追加したいと思います:
Typeface mFont = Typeface.createFromAsset(getAssets(), "fonts/BPreplay.otf");
ViewGroup root = (ViewGroup)findViewById(R.id.myrootlayout);
setFont(root, mFont);
/*
* Sets the font on all TextViews in the ViewGroup. Searches
* recursively for all inner ViewGroups as well. Just add a
* check for any other views you want to set as well (EditText,
* etc.)
*/
public void setFont(ViewGroup group, Typeface font) {
int count = group.getChildCount();
View v;
for(int i = 0; i < count; i++) {
v = group.getChildAt(i);
if(v instanceof TextView || v instanceof Button /*etc.*/)
((TextView)v).setTypeface(font);
else if(v instanceof ViewGroup)
setFont((ViewGroup)v, font);
}
}
レイアウトのルートに渡すと、そのレイアウト内のビュー(またはifステートメントに追加したその他のビュー)が再帰的にチェックされ、IDで指定しなくてもフォントが設定されますTextView
。Button
もちろん、これはすべてのビューにフォントを設定することを前提としています。