2013-02-27 46 views

回答

11

您需要自定義字體,然後才能執行此操作:

Typeface mFont = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf"); 
MyTextView.setTypeface(mFont); 

您必須在資產文件夾中創建「字體」文件夾。把你的字體放在那裏。
您當然也可以創建自定義TextView。如果你喜歡,請參考this answer,我給了一段時間。

+1

由於其完全爲我工作。 – 2013-02-27 13:49:14

7
<TextView 
style="@style/CodeFont" 
android:text="@string/hello" /> 

你需要做的是codefont風格:

<?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> 

直接從:http://developer.android.com/guide/topics/ui/themes.html

0

您可以創建一個layout.xml文件,其中包含您的textview。例如:

textView.xml 

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
style="@android:style/Holo.ButtonBar" > 

如果你不想要這個,那麼你可以創建自定義樣式。事情是這樣的:

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
    <style name="Custom" parent="@android:style/TextAppearance.Large" > 
     <item name="android:typeface">monospace</item> 
    </style> 
</resources> 

,並在佈局文件中改變風格的東西,如:

style="@style/Custom" 
3

還有另一種方式,如果你想改變它在許多TextViews,使用類:

public class MyTextView extends TextView { 

public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(); 
} 

public MyTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
} 

public MyTextView(Context context) { 
    super(context); 
    init(); 
} 

private void init() { 
    if (!isInEditMode()) { 
     Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Ubuntu-L.ttf"); 
     setTypeface(tf); 
    } 
} 

}

,並在佈局取代:

<TextView 
... 
/> 

有了:

<com.WHERE_YOUR_CLASS_IS.MyTextView 
... 

/> 
相關問題