2010-04-01 6 views
26

Ich definiere eine Stil XML für meine Android App. Ich habe einige TTF-Dateien, die ich verwenden möchte, wie kann ich die Schrift einstellen, um diese Dateien als die Schriftart im Gegensatz zu den allgemeinen "Sans", "Serif" & "Monospace" zu verwenden. DankeSpezifische Schriftart in einer styles.xml setzen

Antwort

47

Sie können benutzerdefinierte Schriftarten nur über Java-Code verwenden, nicht durch Layout-XML oder Stile/Themen - Entschuldigung!

+0

Thanks a lot Ich schätze es. – NickTFried

+0

@ SergiCastellsaguéMillán: Diese Frage war für die Verwendung von TTF-Schriftarten. Die Antwort, die Sie verlinkt haben, ist für alles ** aber ** TTF-Fonts. – CommonsWare

+1

Da dies eine alte Antwort ist; Ist dir bewusst, dass sich in späteren Versionen, @CommonsWare, etwas geändert hat? Ich würde auch gerne Schriften ändern können, indem ich es in einem ''-Dokument definiere. –

7
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium"> 
     <item name="android:layout_width">fill_parent</item> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:textColor">#00FF00</item> 
     <item name="android:typeface">monospace</item> 
    </style> 
</resources> 
+2

was ist monspace hier ??? Was ist, wenn ich eine .ttf-Datei habe – sheetal

+1

https://en.wikipedia.org/wiki/Monospaced_font ist eine Art von Schriftart –

+1

Was ist, wenn ich Sans-Serif verwenden möchte? – Senhor

16

TextViewPlus.java nützlich sein kann:

package com.example; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.widget.TextView; 

public class TextViewPlus extends TextView { 
    private static final String TAG = "TextView"; 

    public TextViewPlus(Context context) { 
     super(context); 
    } 

    public TextViewPlus(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setCustomFont(context, attrs); 
    } 

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setCustomFont(context, attrs); 
    } 

    private void setCustomFont(Context ctx, AttributeSet attrs) { 
     TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus); 
     String customFont = a.getString(R.styleable.TextViewPlus_customFont); 
     setCustomFont(ctx, customFont); 
     a.recycle(); 
    } 

    public boolean setCustomFont(Context ctx, String asset) { 
     Typeface tf = null; 
     try { 
     tf = Typeface.createFromAsset(ctx.getAssets(), asset); 
     } catch (Exception e) { 
      Log.e(TAG, "Could not get typeface: "+e.getMessage()); 
      return false; 
     } 

     setTypeface(tf); 
     return true; 
    } 

} 

attrs.xml: (in res/Werte)

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="TextViewPlus"> 
     <attr name="customFont" format="string"/> 
    </declare-styleable> 
</resources> 

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:foo="http://schemas.android.com/apk/res/com.example" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <com.example.TextViewPlus 
     android:id="@+id/textViewPlus1" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:text="@string/showingOffTheNewTypeface" 
     foo:customFont="saxmono.ttf"> 
    </com.example.TextViewPlus> 
</LinearLayout> 

Sie würden setzen "saxmono.ttf" im Vermögen Ordner. Erstellen Sie einen Ordner mit dem Namen ‚font‘ und Ihre Ttf im Inneren:

Verwandte Themen