Android字体样式修改

效果图

准备字体

Download Alibaba Sansicon-default.png?t=M4ADhttps://done.alibabadesign.com/puhuiti2.0比如阿里巴巴普惠体

字体使用

 将字体文件放到res/font文件夹下,如果没有font文件,则新建一个

引用字体

 直接用getResource的方式加载字体样式

设置字体

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() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        textView.setTypeface(fontBeans.get(i).getId());
    }
    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
    }
});

猜你喜欢

转载自blog.csdn.net/mozushixin_1/article/details/125180855