Android中给控件设置字体
来源:互联网 发布:度小月担仔面 知乎 编辑:程序博客网 时间:2024/05/22 00:13
Android中给控件设置字体
1.步骤:
①创建改变字体工具类
public class FontUtil { static public void changeFonts(Context context, ViewGroup root, String font, int size,int color){ Typeface tf = Typeface.createFromAsset(context.getAssets(),font); for (int i=0;i<root.getChildCount();i++){ View v = root.getChildAt(i); if (v instanceof TextView){ ((TextView)v).setTypeface(tf);// ((TextView)v).setTextSize((int)(size*metrics.density)); ((TextView)v).setTextColor(color); }else if (v instanceof Button){ }else if (v instanceof EditText){ ((TextView)v).setTypeface(tf); ((TextView)v).setTextColor(color); }else if (v instanceof ViewGroup){ changeFonts(context, (ViewGroup)v, font, size, color); } } } static public void changeFont(Context context, View element, String font, int size, int color){ Typeface tf = Typeface.createFromAsset(context.getAssets(),font); if (element instanceof TextView) { ((TextView) element).setTypeface(tf); ((TextView) element).setTextSize(size); ((TextView) element).setTextColor(color); }else if(element instanceof EditText){ ((EditText)element).setTypeface(tf); } }}
②在assets文件夹中存放 .ttf字体文件
③使用:
// 给单个view设置字体Button myButton = (Button)findViewById(R.id.myButton);FontUtil.changeFont(context, myButton, "fontFile.ttf", 20, Color.BLACK);// 给viewGroup设置字体LinearLayout MyLinearLayout = (LinearLayout)findViewById(R.id.myLinearLayout);changeFonts(context,(ViewGroup)LinearLayout,"fontFile.ttf", 15, Color.BLUE);
阅读全文
0 0
- Android中给控件设置字体
- Android中给控件设置动画效果
- android-给字体设置投影
- MFC如何给控件设置字体
- Android 控件设置字体【Typeface】
- android 中如何设置控件的字体和背景样式
- Android中字体设置
- Android中设置字体颜色
- Android中字体设置Font
- Android中设置文字字体
- android中 字体颜色设置
- vc中控件字体的设置
- MFC控件中字体的设置
- MFC控件中字体的设置
- wp7 中Panorama控件 title 字体设置
- wp7 中Panorama控件 title 字体设置
- vc中控件字体的设置
- Android中如何给TextView添加下划线、设置不同字体和颜色
- ntp导致其他线程卡顿原因总结
- CentOS下Nginx+fastcgi+python2搭建web.py服务环境
- Freemarker中如何遍历List
- JAVA栈与堆的区别
- bootstrap网格布局原理解析
- Android中给控件设置字体
- iOS性能优化Tips
- 网络返回解析 response.characterset iso-8859-1 gzip 解压
- Linux 开发中的常用命令
- android 屏幕
- 从普通DLL中导出C++类 – dllexport和dllimport的使用方法
- QTableWidget 详细使用
- 结构体 的优先队列 【模板】
- 【pandas】[5] 数据行列转置,数据透视(stack,unstack,pviot,pviot_table)