做項目的時候,往往裏面含有的字體是不好看的,所以我這裏教教大家怎麼去修改原本的字體
當然第一步,你選擇你喜歡的字體,可以去網上下載字體,這個很簡單。
第一步,新建項目,這些你們應該都會了,我就不多說前面的步驟了
第二步,
把下載好的字體,放入assets的目錄下面
第三步,
新建一個類名叫MyApplication(初始化)
爲什麼要初始化呢:做過項目的人都知道,如果你不初始化的話,你每次都要去找,那樣是不是很浪費資源?所以,我在這裏建議你們初始化
package com.ooyuan.myapplication;
import android.app.Application;
import android.graphics.Typeface;
public class MyApplication extends Application {
>
public static Typeface tf;
@Override
public void onCreate() {
super.onCreate();
tf=Typeface.createFromAsset(getAssets(), "fonts/Lantinghei.ttf");
}
}
別忘記在AndroidManifest.xml添加初始化。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.XD.wqjjj"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:name="com.XD.myapplication.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Logo"
android:label="@string/title_activity_logo"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
別寫錯了你的包名字,我這裏是com.XD.com.XD.myapplication.MyApplication這個類名字
第四步,
自定義一個TextView,然後給這個TextView上字體。
package com.XD.zdyview;
import com.XD.myapplication.MyApplication;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class mytextview extends TextView{
public mytextview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public mytextview(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public mytextview(Context context) {
super(context);
init(context);
}
private void init(Context context){
//AssetManager mgr=context.getAssets();
//Typeface tf=Typeface.createFromAsset(mgr, "fonts/Lantinghei.ttf");
setTypeface(MyApplication.tf);
}
}
第五步:
<com.XD.zdyview.mytextview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="玩去張家界"
android:layout_marginRight="10dp"
/>
就是集成這個自定義的控件!
OK,是不是很簡單?有什麼不懂的,可以直接問我,也可以加羣QQ552123831
demo在下面鏈接
http://download.csdn.net/detail/zhenliang5553/8853075