美文网首页
Android创建assets文件夹并使用字体图标(Icon F

Android创建assets文件夹并使用字体图标(Icon F

作者: 十方天仪君 | 来源:发表于2017-09-23 15:31 被阅读94次
首先我们要知道如何在AS中新建引用assets文件

1.将项目切换到Project模式

Project.png
2.要注意assets文件夹要跟Java文件在同一目录下,都包含在main的文件下面。 image.png

3.右键单击main目录,选择New>Folder>Assets Folder.

image.png

4.这样就创建成功assets文件夹,可以开始我们使用字体图标

image.png
5.生成.ttf格式的字体文件
这里我一般获取字体图标都是通过阿里巴巴矢量图标库因为这里面不仅有很多各式各样的字体图标,并且很多都十分适合开发者,用起来也很方便。 image.png

6.需要点击紫色的图标新建一个自己个项目才可以下载完整版的字体图标,不然单独下载界面上的图标无法生成ttf文件格式,新建完成可以添加图标,也可以上传svg图标(必须是svg格式的图标才可以上传),将项目的图标添加完成就可以下载了。


image.png

7.下载字体图标里之后生成的zip,解压之后会有这样的一个文件


ttf格式的字体图标
8.我在工程中怎么找到这些图标?
正如前面所说 我们其实已经知道怎么来代表一个图标了,但是还是需要知道怎么在工程中用,其实也很简单我们在res --> values --> string.xml中添加字符串如图: string 添加样式.png

这个样式是如何获取的呢

很简单就是点击如图复制代码,再在string中粘贴即可。

string中的字符串

9.字体文件的使用

image.png image.png

这些图标怎么设置颜色和具体大小?

就相当于对这个其实变成了字一样进行其TextView的文字的颜色大小设置一样
可以在xml中进行设置

        android:id="@+id/tvShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:gravity="center"
        android:textColor="#00ff00"
        android:textSize="80sp"/>

也可以动态用代码在activity中设置

tvShow.setTextSize(80); //设置大小
tvShow.setTextColor(Color.parseColor("#00ff00")); //设置颜色

10.自定义FontIconView来进行代码的代码优化

理解的上面的我们就可以自定义一个字体图标的控件 FontIconView
在工程目录下新建一个继承TextView 的class 起名为FontIconView
public class FontIconView extends TextView {

    public FontIconView(Context context) {
        super(context);
        init(context);
    }

    public FontIconView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FontIconView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化init
     */
    public void init(Context context) {
        Typeface font = Typeface.createFromAsset(context.getAssets(), "iconfont.ttf");
        this.setTypeface(font);
    }
}

在xml中引用

   <cn.lemon.countdownview.FontIconView
        android:id="@+id/tv_show3"
        android:layout_centerInParent="true"
        android:layout_below="@+id/tv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />

在activity中使用

     FontIconView tv_show1 = (FontIconView) findViewById(R.id.tv_show3);
        tv_show1.setText(getResources().getString(R.string.font5));

动态代码设置

 FontIconViewfontView= (FontIconView) findViewById(R.id.fontView);
        fontView.setText(getResources().getString(R.string.font1));
        fontView.setTextSize(15);
        fontView.setTextColor(Color.parseColor("#ff0000"));  //设置颜色
这样会不会发现以前比较困难的图片自适应问题是不是变得迎刃而解了,并且在这之中,我们会发现这个自定义FontIconView之后代码的使用率也很高,并且什么样的颜色和大小的只要简简单单的设置一下就好了,所以相信对开发者而言,这种功能性十分方便,并且开发过程也变得简单多了。

参考自:
想飞的喵星人
主要是自己备忘

相关文章

网友评论

      本文标题:Android创建assets文件夹并使用字体图标(Icon F

      本文链接:https://www.haomeiwen.com/subject/hvycextx.html