我已經閱讀了一些文章並在Google上搜索過,但是我沒有做到。Android中的自定義字體
我的問題是關於字體面。
在Android中,也有"android:typeface"
只有4個屬性:正常,三世,襯線,等寬。
那麼做我必須做我的應用程序中使用「宋體」?
請建議我在我的Android應用程序中使用該字體的正確道路。
我已經閱讀了一些文章並在Google上搜索過,但是我沒有做到。Android中的自定義字體
我的問題是關於字體面。
在Android中,也有"android:typeface"
只有4個屬性:正常,三世,襯線,等寬。
那麼做我必須做我的應用程序中使用「宋體」?
請建議我在我的Android應用程序中使用該字體的正確道路。
這是一個簡單的例子......在您的項目稱爲assets/fonts/
然後將其粘貼TTF字體文件(在這種情況下Verdana.ttf)的根目錄創建一個文件夾。然後,如果你想申請的是字體,說TextView
,請執行下列操作:
import android.graphics.Typeface;
public class FontSampler extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
tv.setTypeface(face);
}
}
這個例子是從ComonsWare書(馬克·墨菲寫的)拍攝。您可以download the full example from GitHub。
有沒有辦法將此應用於所有文本應用程序而不是單個文本視圖? – 2012-09-12 16:42:09
您可以通過xml佈局設置控件的字體嗎? – 2013-03-27 11:55:50
是和不是。你不能開箱即用......但是你可以擴展'TextView'來添加這樣的功能。 – Cristian 2013-03-27 18:31:52
您可以在https://github.com/neopixl/PixlUI
進口他們的.jar使用PixlUI和XML使用它
<com.neopixl.pixlui.components.textview.TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
pixlui:typeface="GearedSlab.ttf" />
我試過你的庫,但它似乎沒有對android的影響:textStyle =「bold」如果我將TextView更改爲pixlui TextView。 (v 1.0.5) – mrmoment 2014-10-10 03:48:44
// My example show you how to change fonts into a normal textView or list view
create a fonts folder into your assets dir of android and copy your custom font in that ..
assets/fonts/monaco.ttf
// Font path
String fontPath = "fonts/monaco.ttf";
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// CASE 1 : Inside your list view
holder.name = (TextView) convertView
.findViewById(R.id.textView_cityName);
// set name of text in each row
holder.name.setText(CitiesNames.get(position));
// set the type of font you want to set
holder.name.setTypeface(tf);
// CASE 2 : Inside your text view
TextView tx = (TextView)findViewById(R.id.textview1);
tx.setTypeface(tf);
//vKj
TextView textView = (Textview) findViewById(R.id.mytext);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
textView.setTypeFace(face);
你爲什麼又重複了代碼/答案? – 2015-02-10 08:55:49
您可以使用簡單的EasyFonts第三方庫設置各種自定義字體的到您的TextView
。通過使用這個庫,您不必擔心下載並將字體添加到assets/fonts文件夾中。還有關於字體對象的創建。
此庫不提供宋體字體的臉。
但提供以下字體。您可能想使用哪個。
只需:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
我是該庫的作者。
好吧!
這個問題是很老,但仍然如果有人對如何通過XML代碼自定義字體應用於所有Textviews尋找答案(2015年),直接看下面:
首先:
我們需要添加自定義字體內部資產文件夾中的應用程序目錄中:
的.ttf或雜項文件都工作在Android的
二:
創建類CustomTextView延伸的TextView象下面這樣:
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setTypeface(Typeface tf) {
super.setTypeface(FontCache.getFont(getContext(),"fonts/<font_name>"));
}
}
第三:
CustomTextView的setTypeface內正在使用 FontCache類()method.Purpose是使用HashMap來執行基本的字體緩存:
public class FontCache {
private static Map<String,Typeface> fontMap = new HashMap<String,Typeface>();
public static Typeface getFont(Context context,String fontname){
if(fontMap.containsKey(fontname)){
return fontMap.get(fontname);
}
else{
Typeface tf = Typeface.createFromAsset(context.getAssets(),fontname);
fontMap.put(fontname,tf);
return tf;
}
}
}
第四個: [最後一步] 我們現在要做的就是直接在我們的xml文件中使用自定義字體textview的地方使用CustomTextView:
<<package_name>.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Font Text"
android:textSize ="18sp"
android:textAppearance="?android:textAppearanceSmall"
android:id="@+id/custom_txt"
/>
對不起,如果這已經發布在SO的某處。只是想分享,如果它可以幫助別人!
要改變你的應用程序的(自定義)字體全球,看看Calligraphy
只需書法添加到您的gradle.build和下面的代碼片段添加到您的Application.onCreate()
:
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/MyCustomFont.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
和在每一個活動添加以下內容:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
這是所有你需要做的在全球範圍內改變字體在你的應用程序。有關更多詳細信息,請參閱文檔。
檢查此文章:[http://stackoverflow.com/questions/2888508/how-to-change-the-font-on-the-text-view-in-android](http://stackoverflow.com/questions/2888508/how-to-change-the-font-on-the-text-view-in-android) – Praveen 2010-07-08 12:50:52
還請檢查這一個:http://stackoverflow.com/a/14558090/693752 – Snicolas 2013-01-28 08:25:41
http:/ /stackoverflow.com/questions/9030204/how-to-use-custom-font-in-android-xml/9035924#9035924 – Vins 2013-05-02 13:31:30