Android字体样式修改

效果图

准备字体

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

这里字体的下拉列表,用到了Spinner 控件,给Spinner 设置ArrayAdapter,添加setOnItemSelectedListener监听

不能用setOnItemClickListener这个监听,否则会报错的。

  • 11
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会写代码的猴子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值