自定义TextView显示.ttf文件中的字符
来源:互联网 发布:臭氧灯能除螨虫知乎 编辑:程序博客网 时间:2024/06/05 14:15
利用Typeface
显示.ttf文件中的字符在TextView上面。
效果图:
代码如下:
/** * 作者:僧格洛卓 * 描述:TypefaceTextView */ public class TypefaceTextView extends TextView { public TypefaceTextView(Context context) { super(context); init(context, null); } public TypefaceTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context, attrs); } private void init(Context context, AttributeSet attrs) { // 自定义属性 TypedArray mArray = context.obtainStyledAttributes(attrs, R.styleable.TypefaceTextView); String mTypefacePath = mArray.getString(R.styleable.TypefaceTextView_typefacePath); String mTypefaceUnicode = mArray.getString(R.styleable.TypefaceTextView_typefaceUnicode); mArray.recycle(); if (!TextUtils.isEmpty(mTypefacePath) && !TextUtils.isEmpty(mTypefaceUnicode)) { setTypeface(Typeface.createFromAsset(context.getAssets(), mTypefacePath)); setText(mTypefaceUnicode); } } }
自定义属性:
<declare-styleable name="TypefaceTextView"> <!--assets文件夹下的.ttf文件地址--> <attr name="typefacePath" format="string" /> <!--Unicode码--> <attr name="typefaceUnicode" format="reference|string" /> </declare-styleable>
在布局中使用:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:typefacetext="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:gravity="center" android:orientation="horizontal"> <com.example.app.view.widget.TypefaceTextView android:id="@+id/navigation_home" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:textColor="@color/gray" android:textSize="25dp" typefacetext:typefacePath="navigation/home_page_navigation.ttf" typefacetext:typefaceUnicode="@string/icon_home" /> <com.example.app.view.widget.TypefaceTextView android:id="@+id/navigation_search" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:textColor="@color/gray" android:textSize="25dp" typefacetext:typefacePath="navigation/home_page_navigation.ttf" typefacetext:typefaceUnicode="@string/icon_search" /> <com.example.app.view.widget.TypefaceTextView android:id="@+id/navigation_message" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:textColor="@color/gray" android:textSize="25dp" typefacetext:typefacePath="navigation/home_page_navigation.ttf" typefacetext:typefaceUnicode="@string/icon_message" /> <com.example.app.view.widget.TypefaceTextView android:id="@+id/navigation_user" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:textColor="@color/gray" android:textSize="20dp" typefacetext:typefacePath="navigation/home_page_navigation.ttf" typefacetext:typefaceUnicode="@string/icon_user" /></LinearLayout>
属性说明:
typefacetext:typefacePath="navigation/home_page_navigation.ttf"
navigation/home_page_navigation.ttf 为assets文件夹下.ttf文件
typefacetext:typefaceUnicode="@string/icon_home"
@string/icon_home 为string中的Unicode字符码
案例中Unicode对应的string
<resources> <string name="icon_home">></string> <string name="icon_search">></string> <string name="icon_message"></string> <string name="icon_user"></string> </resources>
点击下载案例中ttf文件
0 0
- 自定义TextView显示.ttf文件中的字符
- 自定义TextView 调用ttf格式字体
- android EditText,textview显示 特殊字符及自定义字体
- TextView显示html文件中的图片
- 显示文件中的所有字符
- C# 从TTF文件加载自定义字体
- CSS中调用ttf文件中的字体
- TextView显示html文件中的图片,为什么显示不出来
- 字符根据字体文件(ttf)转换成字型(glyph)
- cocos2d-x自定义ttf文件支持想要的英文中文。压缩ttf大小
- Android控件TextView显示html文件中的图片
- Android控件TextView显示html文件中的图片
- Android控件TextView显示html文件中的图片
- Android 添加自定义的.ttf字库文件到系统
- 自定义TextView显示偏移问题
- 自定义TextView显示指定行数
- 自定义Textview显示自动换行
- 从TTF文件中读取文字的OutLine并显示
- mybatis中插入数据自动返回自增长id的配置
- Qt软件开发文档8---登陆界面的绘制及其封装
- IntelliJ IDEA 乱码解决方案 (项目代码、控制台等)
- poj 2318 TOYS(叉积)
- Lintcode18 Subsets II solution 题解
- 自定义TextView显示.ttf文件中的字符
- 鼠标掠过显示图片的demo
- poj 2182 Lost Cows 暴力或者用线段树 + poj2828Buy Tickets
- linux解压-压缩命令
- 文章标题
- 操作系统:并发和并行
- 安装 Kali Linux 后需要做的 20 件事
- Dubbo原理简单分析
- 用python脚本实现自动部署环境(二)