做项目的时候,往往里面含有的字体是不好看的,所以我这里教教大家怎么去修改原本的字体
当然第一步,你选择你喜欢的字体,可以去网上下载字体,这个很简单。
第一步,新建项目,这些你们应该都会了,我就不多说前面的步骤了
第二步,
把下载好的字体,放入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.youkuaiyun.com/detail/zhenliang5553/8853075