您的位置:首页 > 移动开发 > Android开发

android实现ios形式的字体,实现各种自定义的字体

2016-12-20 10:37 330 查看
这段时间整个人都是处在一种颓废的状态,不想做任何事情,每天就是行尸般的上班,下班,然后在家玩游戏,状态是差的要死,脑子感觉就是一片混沌态,也不知道要写些啥了,真的是太可怕,为了防止延续,决定先写篇基础的东西压压惊吧,这样的状态真的不想再有了。希望以后也不要有了吧。

android系统自带的4种字体,通过 分别为android:typeface=:“normal”,“sans”, “serif”, “monospace",样式通过android:textStyle=""normal ,bold,italic ,这个对于老外的英文来说基本是可以用了,也不是很难看,可是用在了汉子的形式上,感觉是bold还是粗了些,所以我们还是有这个必要去实现下不同的字体,

1.准备工作

a.准备要实现字体模版

其实在实现这个字体的时候呢 ,首先你要有一个这中字体的模版,也就是.ttf格式的文件(这个项目中提供了两种字体,)通过下面的两个链接

http://download.csdn.net/detail/u010778159/8557559 华文黑体

http://download.csdn.net/detail/u013424496/9715596 平黑体

当然如果你是Windows 系统,可以在C:\Windows\Fonts找到,它本身就提供了很多字体文件,

样式 :显示前面的12,18应该就是px为单位的时候的字体样式



b.项目的准备

按照下面箭头步骤把上面现在的.ttf文件放到创建的文件夹下 ,创建的文件夹名字叫 assets ,不要写错奥



ok,准备好上面的步骤,你就准备好了一半了

2.自定义view ,拿到资源中的字体

TextView ,重新定义字体其实也就是使用了setTypeface()的方法,对于里面的参数的话就是上面第一步准备的字体了,通过

Typeface.createFromAsset(getContext().getAssets(), "STXINGKA.TTF")

拿到就OK啦,自定义的话可以通过如下方法,

public class MyTextView extends TextView {
public MyTextView(Context context) {
super(context);
}

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "STXINGKA.TTF"));
}

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


然后在xml中使用

<pszhcom.custom.customtextstyle.MyTextView
android:id="@+id/mytv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#E12A54"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:text="you !"/>


就OK了,

当然你也可以在activity中直接调用

((TextView)findViewById(R.id.tv)).setTypeface(Typeface.createFromAsset(MainActivity.this.getAssets(),"STXINGKA.TTF"));


OK了,在上一张效果图吧(有些骚气





最后的话,如果这中字体是大量使用的话,建议把

Typeface.createFromAsset(MainActivity.this.getAssets(),"STXINGKA.TTF")

最为一个变量放到 application里面










                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息