效果图
准备字体
Download Alibaba Sans比如阿里巴巴普惠体,也可以其他网站找一些对应的资源。
字体使用
将字体文件放到res/font文件夹下,如果没有font文件,则新建一个。
添加到font路径下的字体文件,明明不能是汉字,也不能大写字母开头(即使可能不提示,但是仍然不建议这样使用)
引用字体
直接用getResource的方式加载字体样式
如果要在布局文件中直接使用,可以给控件添加
android:fontFamily="@font/****"
这样也可以直接引用到字体样式。
设置字体
代码中动态设置某一个文字控件的样式,可以这样用
textView.setTypeface(字体);
功能代码
实体类主要是把字体样式描述出来
//实体类
public class FontBean {private String name;private Typeface id;public FontBean(String name, Typeface id){this.name = name;this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Typeface getId() {return id;}public void setId(Typeface id) {this.id = id;}}//加载资源
public List<FontBean> getTTFResource(Context context) {List<FontBean> resources = new ArrayList<>();FontBean fontBean = new FontBean("阿里巴巴普惠体", context.getResources().getFont(R.font.alibabapupuiti_medium));resources.add(fontBean);FontBean fontBean1 = new FontBean("影字体", context.getResources().getFont(R.font.impact));resources.add(fontBean1);FontBean fontBean2 = new FontBean("迷你繁启体", context.getResources().getFont(R.font.maobiqigong));resources.add(fontBean2);FontBean fontBean3 = new FontBean("苹果 简 中黑体", context.getResources().getFont(R.font.appblackjian));resources.add(fontBean3);FontBean fontBean4 = new FontBean("苹果 繁 中黑体", context.getResources().getFont(R.font.appblackzhongheijian));resources.add(fontBean4);FontBean fontBean5 = new FontBean("苹果 简 极细体", context.getResources().getFont(R.font.appjixijian));resources.add(fontBean5);FontBean fontBean6 = new FontBean("苹果 繁 极细体", context.getResources().getFont(R.font.appjixifan));resources.add(fontBean6);FontBean fontBean7 = new FontBean("苹果 简 中粗体", context.getResources().getFont(R.font.applezhongheijian));resources.add(fontBean7);FontBean fontBean8 = new FontBean("苹果 繁 中粗体", context.getResources().getFont(R.font.applezhongheifan));resources.add(fontBean8);FontBean fontBean9 = new FontBean("苹果 简 正规体", context.getResources().getFont(R.font.applezhunjian));resources.add(fontBean9);FontBean fontBean10 = new FontBean("苹果 繁 正规体", context.getResources().getFont(R.font.appzhunfan));resources.add(fontBean10);FontBean fontBean11 = new FontBean("苹果 简 纤细体", context.getResources().getFont(R.font.appxianxijian));resources.add(fontBean11);FontBean fontBean12 = new FontBean("苹果 繁 纤细体", context.getResources().getFont(R.font.appxianxifan));resources.add(fontBean12);FontBean fontBean13 = new FontBean("苹果 简 细体", context.getResources().getFont(R.font.appxijianjian));resources.add(fontBean13);FontBean fontBean14 = new FontBean("苹果 繁 细体", context.getResources().getFont(R.font.appxifan));resources.add(fontBean14);return resources;
}//设置字体
List<FontBean> fontBeans = getTTFResource(getActivity());
extView textView = /*从布局获取*/;
Spinner spinner = /*从布局获取*/;
String[] str = new String[fontBeans.size()];
for (int i = 0; i < fontBeans.size(); i++) {str[i] = fontBeans.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, str);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {textView.setTypeface(fontBeans.get(i).getId());}@Overridepublic void onNothingSelected(AdapterView<?> adapterView) {}
});
这里字体的下拉列表,用到了Spinner 控件,给Spinner 设置ArrayAdapter,添加setOnItemSelectedListener监听
不能用setOnItemClickListener这个监听,否则会报错的。