首先我想说明一下字体图标的好处,最大的好处就是自适应了,而且是使用TextView 不用去切图,是矢量图 灵活调用

第一步我要说明一下一般字体图标的来源,我这里使用的是  --阿里巴巴矢量图标库 -网址 :http://www.iconfont.cn  (申明这不是广告哈~)

1.首先我们的自己创建一个自己的图标库,可以自己创建一些字体图标,也可以在公共的图标库中加载到自己的库中(这些操作不用我说了吧~)

这个时候我们创建了一个自定义的库(为了保护隐私我特意打了码 啊哈哈哈哈哈哈哈。。。。。),好了回归正题,这时我们点击上面的图片中的   下载到本地  按钮   然后会下载到一个这样的包  

解压这个包会看到里面的一个文件夹  打开,里面会看到这样的目录 仔细看里面有个demo.html,还有个demo.css ,至于css我们先不管,这时打开demo.html

你会看到如下:

这时还有几个问题 :1.我怎么把这些放入我的工程?  2.我在工程中怎么找到这些图标?  3.这些图标怎么设置颜色和具体大小?

首先我们来看一个地方 在网页中 鼠标点击右键 选中查看源

 

这时看到html中的红色方框和网页中的图标是一一对应的 所以这个“ &#xe667 ”其实就是图标,这样就知道了图标在什么地方 ,细心的同学可以发现,网页中每个图标下面都有三行文字,其中第二行就是,所以其实不用看html源码,但是得知道为什么,对吧。

好了知道了这些我们开始完成第一点 :1.我怎么把这些放入我的工程?

解答:

    其实很简单 在刚才解压的目录中 选择后缀名为.ttf的文件放入你的Android工程下的assets文件夹下 这样就可以了(其他的我们不用管)。

2.我在工程中怎么找到这些图标?

解答:  正如前面所说 我们其实已经知道怎么来代表一个图标了,但是还是需要知道怎么在工程中用,其实也很简单我们在res --> values  --> string.xml中添加字符串

如下:

对应的是网页的前三个这样我们做好了第一步。

第二步:

在activity_main.xml中给TextView加上上id

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <!-- activity_main.xml 中用来显示图标 -->
<TextView
android:id="@+id/tvShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>

在MainActivity.java中填写代码

package com.example.ztestfonticon;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); TextView tvShow = (TextView) findViewById(R.id.tvShow);
Typeface font = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
tvShow.setTypeface(font);
tvShow.setText(getResources().getString(R.string.font1));
// tvShow.setText(getResources().getString(R.string.font2)); //可以试试我们添加的其他两个图标
// tvShow.setText(getResources().getString(R.string.font3));
}
}

这样就完成了使用,现在可以看看运行效果了

 看到这个图标显示出来了,表示我们成功了。

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

解答: 其实很简单,你怎么操作TextView 的就可以怎么样操作这个字体图标的大小和颜色

xml中添加大小和颜色:

<TextView
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"/>

java 代码设置:

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

效果:

理解的上面的我们就可以自定义一个字体图标的控件 FontTextView

在工程目录下新建一个继承TextView 的class 起名为FontTextView

FontTextVew.java 代码:

package com.example.views;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView; /**
*
* 自定义字体图标
* @author M.Z
*
*/
public class FontTextView extends TextView{ /*
* 控件在xml加载的时候是调用两个参数的构造函数 ,为了自定义的控件的完整性我们可以
* 都把构造函数写出来
*/
public FontTextView(Context context) {
super(context);
init(context);
} public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
} public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
} /**
* 初始化
* @param context
*/
private void init(Context context) {
//设置字体图标
Typeface font = Typeface.createFromAsset(context.getAssets(), "iconfont.ttf");
this.setTypeface(font);
}
}

在xml中使用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/tvShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/> <com.example.views.FontTextView
android:id="@+id/fontView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/> </LinearLayout>

MainActivity.java

package com.example.ztestfonticon;

import com.example.views.FontTextView;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView; public class MainActivity extends Activity { private FontTextView fontView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); TextView tvShow = (TextView) findViewById(R.id.tvShow);
Typeface font = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
tvShow.setTypeface(font);
tvShow.setText(getResources().getString(R.string.font1));
// tvShow.setText(getResources().getString(R.string.font2)); //可以试试我们添加的其他两个图标
// tvShow.setText(getResources().getString(R.string.font3)); tvShow.setTextSize(80); //设置大小
tvShow.setTextColor(Color.parseColor("#00ff00")); //设置颜色 fontView = (FontTextView) findViewById(R.id.fontView);
fontView.setText(getResources().getString(R.string.font3));
fontView.setTextSize(80);
fontView.setTextColor(Color.parseColor("#ff0000")); //设置颜色
}
}

效果:

demo下载地址:  http://pan.baidu.com/s/1i4Adyip

Android怎么使用字体图标 自定义FontTextView字体图标控件-- 使用方法的更多相关文章

  1. Android开源中国客户端学习 (自定义View)左右滑动控件ScrollLayout

    左右滑动的控件我们使用的也是非常多了,但是基本上都是使用的viewpager 等 android基础的控件,那么我们有么有考虑过查看他的源码进行定制呢?当然,如果你自我感觉非常好的话可以自己定制一个, ...

  2. android - 自定义(组合)控件 + 自定义控件外观

    转载:http://www.cnblogs.com/bill-joy/archive/2012/04/26/2471831.html android - 自定义(组合)控件 + 自定义控件外观   A ...

  3. Android创建自定义的布局和控件

    Android的自带布局有framelayout.linerlayout.relativelayout,外加两个百分比布局,但是这些无法灵活的满足我们的需要,所以我们要自己自定义并引入自己的布局.首先 ...

  4. Android Studio教程06-布局,监听器以及基本控件

    目录 2. 监听器 3. 布局 3.1. 布局分类 (1). Linear Layout (2). Relative Layout (3). ListView (4). Grid View 4. 其他 ...

  5. 获取 AlertDialog自定义的布局 的控件

    AlertDialog自定义的布局 效果图: 创建dialog方法的代码如下: 1 LayoutInflater inflater = getLayoutInflater(); 2 View layo ...

  6. 使用VideoView自定义一个播放器控件

    介绍 最近要使用播放器做一个简单的视频播放功能,开始学习VideoView,在横竖屏切换的时候碰到了点麻烦,不过在查阅资料后总算是解决了.在写VideoView播放视频时候定义控制的代码全写在Actv ...

  7. Pro Android 4 第六章 构建用户界面以及使用控件(一)

         目前为止,我们已经介绍了android的基础内容,但是还没开始接触用户界面(UI).本章我们将开始探讨用户界面和控件.我们先讨论一下android中UI设计的一般原理,然后我们在介绍一下an ...

  8. 【转】UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)

    原文地址:http://blog.csdn.net/zhubaitian/article/details/39777951 在本人之前的一篇文章<<Appium基于安卓的各种FindEle ...

  9. UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)

    在本人之前的一篇文章<<Appium基于安卓的各种FindElement的控件定位方法实践和建议>>第二章节谈到Appium可以通过使用UIAutomator的方法去定位And ...

随机推荐

  1. 【温故而知新-Javascript】使用canvas元素(第一部分)

    1. 开始使用 canvas 元素 canvas 元素非常简单,这是指它所有的功能都体现在一个JavaScript对象上,因此该元素本身只有两个属性:width 和 height. canvas 元素 ...

  2. Stanford机器学习笔记-10. 降维(Dimensionality Reduction)

    10. Dimensionality Reduction Content  10. Dimensionality Reduction 10.1 Motivation 10.1.1 Motivation ...

  3. Mantis1.2.19 在Windows 平台上的安装配置详解

    安装环境: WindowsXP 32 Apache2.2.22+PHP5.4.39+MySQL5.5.28 一.简介 MantisBT是由PHP开发的.基于WEB的缺陷跟踪系统,并采用开源数据库MyS ...

  4. Maven与Ant使用reportNG代替testng

    大家在使用TestNG时一定会发现其本身的报告生成不但简陋而且相当的不美观,而ReportNG正是一款为TestNG量身定做的报告生成插件,其报告美观.简约.清晰.漂亮的特点让很多TestNG开始慢慢 ...

  5. Linux常用命令学习

    1.ls命令 就是list的缩写,通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文件权限(包括目录.文件夹.文件权限)查看目录信息等等 常用参数搭配: ls -a 列出目录所有文 ...

  6. LFI漏洞利用总结

    主要涉及到的函数 include(),require().include_once(),require_once() magic_quotes_gpc().allow_url_fopen().allo ...

  7. stl学习(二)集合 set 的使用

    set集合容器底层由红黑树实现,是平衡二叉搜索树. 相对stl中的list.deque效率更高. 注意:由于集合 的 性质,单纯的 set 不允许重复的元素 初始化 / 清空 函数 : clear() ...

  8. 深入理解Java之线程池

    原作者:海子 出处:http://www.cnblogs.com/dolphin0520/ 本文归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...

  9. [资料]mysql实现地理位置搜索

    mysql实现地理位置搜索 使用mysql来实现lbs(地理位置服务)功能 Mysql 地区经纬度 查询

  10. distributed caching for .net applications

    distributed caching for .net applications fast, scalable distributed caching with meaningful perform ...