2013-02-27 78 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 
... 

/> 
相关问题