⑴ 如何在android使用图标字体
1. 字体文件制作
进入网站https://icomoon.io/->注册->登录->点击IconMoon App
进入字体生成页面,点击菜单的Manage Projects项
进入项目管理页面,看到项目列表,点击Load
进入项目开始选择图标,选择后点击右下角的Generate Font
进入生成预览页面,点击Download
解压文件后,复制fonts/icomoon.ttf字体文件到Android工程的assets/iconify/egow-icon.ttf
字体制作已完成。Hello, I’m John : )
2. Android应用中如何读取字体
使用开源项目android-iconify
新建Icon类,实现Icon接口
import com.joanzapata.iconify.Icon;
public enum EGOWIcons implements Icon {
ic_glass('uE92D'),
ic_adjustable_bed_frame('uE900'),
ic_air_conditioner('uE901'),
ic_line_chart('uE902');
char character;
EGOWIcons(char character) {
this.character = character;
}
@Override
public String key() {
return name().replace('_', '-');
}
@Override
public char character() {
return character;
}
}
其中uE92D编码是对应字体文件水杯的编码
新建Mole类,用于读取字体文件
import com.joanzapata.iconify.Icon;
import com.joanzapata.iconify.IconFontDescriptor;
public class EGOWMole implements IconFontDescriptor {
@Override
public String ttfFileName() {
return "iconify/egow-icon.ttf";
}
@Override
public Icon[] characters() {
return EGOWIcons.values();
}
}
在Application类中加载字体模块
Iconify.with(new EGOWMole());11
开始使用字体图标,iconDrawable继承Drawable类
IconDrawable iconDrawable = new IconDrawable(context, icon);11
Android中调用字体图标已完成。Hello, I’m John : )
3. 如何修改字体样式,包括大小,颜色,透明度
IconDrawable iconDrawable = new IconDrawable(context, icon);
iconDrawable.sizeDp(24); // 大小
iconDrawable.color(color); // 颜色
iconDrawable.setAlpha(222); // 透明度
⑵ 关于android自定义字体,该怎么处理
解决方案
1)Android默认方法 #1
你可以通过ID查找到View,然后挨个为它们设置字体。在单个View的情况下,它看起来也没有那么可怕。
Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/YourCustomFont.ttf");
TextView view = (TextView) findViewById(R.id.activity_main_header);
view.setTypeface(customFont);
但是在很多TextView、Button等文本组件的情况下,我敢肯定你不会喜欢这个方法的。:D
2)Android默认方法 #2
你可以为每个文本组件创建一个子类,如TextView、Button等,然后在构造函数中加载自定义字体。
public class BrandTextView extends TextView {
public BrandTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public BrandTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BrandTextView(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/YourCustomFont_Bold.ttf"));
} else {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/YourCustomFont.ttf"));
}
}
}
然后只需要将标准的文本控件替换成你自定义的就可以了(例如BrandTextView替换TextView)。
<com.your.package.BrandTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View with custom font"/>
<com.your.package.BrandTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="View with custom font and bold typeface"/>
还有,你甚至可以直接在XML中添加自定义的字体属性。要实现这个,你需要定义你自己的declare-styleable属性,然后在组件的构造函数中解析它们。
为了不占篇幅介绍这么基础的东西,这里有一篇不错的文章告诉你怎么自定义控件属性。
http://kevindion.com/2011/01/custom-xml-attributes-for-android-widgets/
在大多数情况下,这个方法还不赖,并且有一些优点(例如,切换字体粗细等等,字体可以在组件xml文件的typeface属性中定义)。但是我认为这个实现方法还是太重量级了,并且依赖大量的模板代码,为了一个替换字体的简单任务,有点儿得不偿失。
3)我的解决方案
理想的解决方案是自定义主题,然后应用到全局或者某个Activity。
但不幸的是,Android的android:typeface属性只能用来设置系统内嵌的字体,而非用户自定义字体(例如assets文件中的字体)。这就是为什么我们无法避免在java代码中加载并设置字体。
所以我决定创建一个帮助类,使得这个操作尽可能的简单。使用方法:
FontHelper.applyFont(context, findViewById(R.id.activity_root), "fonts/YourCustomFont.ttf");
并且这行代码会用来加载所有的基于TextView的文本组件(TextView、Button、RadioButton、ToggleButton等等),而无需考虑界面的布局层级如何。
标准(左)与自定义(右)字体的用法。
Standard (left) and Custom (right) fonts usage.
这是怎么做到的?非常简单:
public static void applyFont(final Context context, final View root, final String fontName) {
try {
if (root instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); i++)
applyFont(context, viewGroup.getChildAt(i), fontName);
} else if (root instanceof TextView)
((TextView) root).setTypeface(Typeface.createFromAsset(context.getAssets(), fontName));
} catch (Exception e) {
Log.e(TAG, String.format("Error occured when trying to apply %s font for %s view", fontName, root));
e.printStackTrace();
}
}
正如你所看到的,所需要做的仅仅是将基于TextView的文本组件从布局中遍历出来而已。
⑶ 如何在Android开发中使用自定义的字体库
Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace",除此之外还可以使用其他字体文件(*.ttf)
方法一:XML中使用android默认字体: android:typeface
方法二:在Android中可以引入其他字体,首先要将字体文件保存在assets/fonts/目录下
//得到TextView控件对象 TextView textView =(TextView)findViewById(R.id.custom);
//将字体文件保存在assets/fonts/目录下,创建Typeface对象
Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf");
//使用字体
textView.setTypeface(typeFace);
⑷ android字体包怎么弄
先下载安卓字体:
http://www.cr173.com/down.asp?id=36990
再进行Root:下载SuperOneClick(PC软件),连接USB,安装好驱动程序,运行SuperOneClick。
安装ROotExplorer文件管理器
http://www.an.com/soft_90800.html#
接下来就要把字体文件复制进去了
⑸ android 怎么加入字体包
工具
Eclipse
方法
在Eclipse中新建Android工程fontdemo
代码只有MainActivity.javaCustomFontTextView.java
布局文件是activity_main.xml。assets下面是使用的字体库文件
⑹ androidStudio中怎么加载字体资源
首先在androidstudio中找到assets文件夹 , (位于serc/main/java下,与res在同一个文件里面) , 找到之后将字体文件(*.ttf)放入这个文件夹下(当然为了规范起见,我们可以再asseets下建立一个font文件夹表示存放字体文件). 如图所示:
其中context.getAssets()方法是用于加载assets文件夹 , "OpenSans-Regular.ttf"是字体文件的相对路径 , 细心的同学可以看见我的assert中有两个相同的字体文件 ,
其中一个在font文件夹下面 , 如果想要加载这个文件夹就要使用:
Typeface tf = Typeface.createFromAsset(getAssets(), "font/OpenSans-Regular.ttf"); 在View中显示这种字体的使用就用setTypeface就行了
⑺ Android:从SD卡中加载自定义的字体
找了下资料
这个应该可以static
Typeface
createFromFile(File
path)
//静态方法,从文件系统构造一个字体,这里参数可以是sdcard中的某个字体文件