Javaコードの「android:fontFamily =」sans-serif-lightに相当するものは何ですか?


106

私の質問は非常に簡単です:

すべてのテキストビューで、現在属性を使用しています

android:fontFamily="sans-serif-light"

ポストHCデバイスでゴージャスな外観を提供します。

残念ながら、これはすべてのウィジェットで動作するわけではなく、私のスピナーの場合、アダプターを上書きする必要があります。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //You can use the new tf here.

    if(convertView == null || convertView.getTag() == null) {
        // new view - populate 
        convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
        convertView.setTag(new Object());
    }

    CheckedTextView spinner_text=(CheckedTextView) convertView.findViewById(android.R.id.text1);
    //Typeface should be set here...
    return spinner_text;
    }
}

それで、コードでまったく同じ結果を得る方法はありますか?

PS:いいえ、アセットフォルダーにタイプフェイスを入れたくありません。システムフォントを使いたいだけです。

回答:


168

setTypeface()and で可能になるはずですTypeface.create()

convertView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));

ドキュメントを参照:

ファミリ名とオプションスタイル情報を指定して、書体オブジェクトを作成します。名前にnullが渡されると、「デフォルト」のフォントが選択されます。結果の書体オブジェクトを照会(getStyle())して、その「実際の」スタイル特性を発見できます。

このコメントにTypeface.create()記載されているように、過度に使用するとメモリに悪影響を与えることに注意してください。提案Hashtableのは良い解決策ですが、資産からあなたの書体を作成しないので、あなたはそれを少し変更する必要があります。


2
スピナーアイテムが多い場合は、メモリの問題に注意してください-回答を少し更新しました。
saschoar 2013年

44

Android 4.1(APIレベル16)およびサポートライブラリ26以降

res-> fontフォルダを使用している場合は、このように使用できます

  val typeface = ResourcesCompat.getFont(Context, R.font.YOUR_FONT)
  TextView.setTypeface(typeface)

20

私の意見では、メモリの問題なしにTextViewにプログラムでシステムフォントを適用する方法がまだあり、それはtextview.setTextAppearancemethod を使用しています:

<style name="styleA">
    <item name="android:fontFamily">sans-serif</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">?android:attr/textColorPrimary</item>
</style>
<style name="styleB">
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">?android:attr/textColorTertiary</item>
</style>


if(condition){
    textView.setTextAppearance(context,R.style.styleA);
}else{
    textView.setTextAppearance(context,R.style.styleB);
}

2
android:fontFamilyAPI 16が必要です
CoolMind

2
を使用しTextViewCompat.setTextAppearance(textView, R.style.styleA)ます。
CoolMind

19

これを使用することにより、xml:でandroid:fontFamilyに類似したfontfamilyを動的に設定できます。

For Custom font:

 TextView tv = ((TextView) v.findViewById(R.id.select_item_title));
 Typeface face=Typeface.createFromAsset(getAssets(),"fonts/mycustomfont.ttf"); 
 tv.setTypeface(face);

For Default font:

 tv.setTypeface(Typeface.create("sans-serif-medium",Typeface.NORMAL));

これらは、使用されるデフォルトのフォントファミリーのリストです。二重引用符の文字列「sans-serif-medium」を置き換えることにより、このいずれかを使用してください

FONT FAMILY                    TTF FILE                    

1  casual                      ComingSoon.ttf              
2  cursive                     DancingScript-Regular.ttf   
3  monospace                   DroidSansMono.ttf           
4  sans-serif                  Roboto-Regular.ttf          
5  sans-serif-black            Roboto-Black.ttf            
6  sans-serif-condensed        RobotoCondensed-Regular.ttf 
7  sans-serif-condensed-light  RobotoCondensed-Light.ttf   
8  sans-serif-light            Roboto-Light.ttf            
9  sans-serif-medium           Roboto-Medium.ttf           
10  sans-serif-smallcaps       CarroisGothicSC-Regular.ttf 
11  sans-serif-thin            Roboto-Thin.ttf             
12  serif                      NotoSerif-Regular.ttf       
13  serif-monospace            CutiveMono.ttf              

「mycustomfont.ttf」はttfファイルです。パスsrc / assets / fonts / mycustomfont.ttfにあります。このデフォルトフォントファミリでデフォルトフォントの詳細を参照できます


13

オプション1 -API 26以降

// Jave
Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);

// Kotlin
val typeface = resources.getFont(R.font.myfont)
textView.typeface = typeface

オプション2 -API 16以降

// Java
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);

// Kotlin
val typeface = ResourcesCompat.getFont(context, R.font.myfont)

Android Developers Guideで完全な有効期限を確認してください。


6

を使用することで可能になります

setTypeface(Typeface tf, int style)TextViewクラスのメソッド。

spinner_text.setTypeface(Typeface.SANS_SERIF,Typeface.NORMAL);

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.