解析Android中使用自定义字体的实现方法

1、Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace

2、在Android中可以引入其他字体 。
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:Android="http://schemas.android.com/apk/res/android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent" >

<TableRow>

<TextView
            Android:layout_marginRight="4px"
            Android:text="sans:"
            Android:textSize="20sp" >
        </TextView>
        <!-- 使用默认的sans字体 -->

<TextView
            Android:id="@+id/sans"
            Android:text="Hello,World"
            Android:textSize="20sp"
            Android:typeface="sans" >
        </TextView>
    </TableRow>

<TableRow>

<TextView
            Android:layout_marginRight="4px"
            Android:text="serif:"
            Android:textSize="20sp" >
        </TextView>
        <!-- 使用默认的serifs字体 -->

<TextView
            Android:id="@+id/serif"
            Android:text="Hello,World"
            Android:textSize="20sp"
            Android:typeface="serif" >
        </TextView>
    </TableRow>

<TableRow>

<TextView
            Android:layout_marginRight="4px"
            Android:text="monospace:"
            Android:textSize="20sp" >
        </TextView>
        <!-- 使用默认的monospace字体 -->

<TextView
            Android:id="@+id/monospace"
            Android:text="Hello,World"
            Android:textSize="20sp"
            Android:typeface="monospace" >
        </TextView>
    </TableRow>
    <!-- 这里没有设定字体,我们将在Java代码中设定 -->

<TableRow>

<TextView
            Android:layout_marginRight="4px"
            Android:text="custom:"
            Android:textSize="20sp" >
        </TextView>

<TextView
            Android:id="@+id/custom"
            Android:text="Hello,World"
            Android:textSize="20sp" >
        </TextView>
    </TableRow>

</TableLayout>

复制代码 代码如下:
// 得到TextView控件对象
TextView textView = (TextView) findViewById(R.id.custom);
// 将字体文件保存在assets/fonts/目录下,www.linuxidc.com创建Typeface对象
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/DroidSansThai.ttf");
// 应用字体
textView.setTypeface(typeFace);

如果想对整个界面的所有控件都应用自定义字体,可以:
复制代码 代码如下:
package arui.blog.csdn.net;

import android.app.Activity;  
import android.graphics.Typeface;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;

public class FontManager {

public static void changeFonts(ViewGroup root, Activity act) {

Typeface tf = Typeface.createFromAsset(act.getAssets(),  
              "fonts/xxx.ttf");

for (int i = 0; i < root.getChildCount(); i++) {  
           View v = root.getChildAt(i);  
           if (v instanceof TextView) {  
              ((TextView) v).setTypeface(tf);  
           } else if (v instanceof Button) {  
              ((Button) v).setTypeface(tf);  
           } else if (v instanceof EditText) {  
              ((EditText) v).setTypeface(tf);  
           } else if (v instanceof ViewGroup) {  
              changeFonts((ViewGroup) v, act);  
           }  
       }

}  
}

时间: 2024-08-02 07:49:40

解析Android中使用自定义字体的实现方法的相关文章

解析Android中使用自定义字体的实现方法_Android

1.Android系统默认支持三种字体,分别为:"sans", "serif", "monospace 2.在Android中可以引入其他字体 . 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:Android="http://schemas.android.com/apk/res/android&qu

android SpannableStringBuilder设置自定义字体

问题描述 android SpannableStringBuilder设置自定义字体 SpannableStringBuilder 怎么设置自定义 字体. 现在是这样 setSpan(new TypefaceSpan(f.mFace), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 不知道 怎么读取assets文件中得字体 来设置 解决方案 读取 Typeface customFont = Typeface.createFromAsset(th

Android中正确使用字体图标(iconfont)的方法_Android

字体图标 字体图标是指将图标做成字体文件(.ttf),从而代替传统的png等图标资源. 使用字体图标的优点和缺点分别为: 优点:       1. 可以高度自定义图标的样式(包括大小和颜色),对于个人开发者尤其适用       2. 可以减少项目和安装包的大小(特别你的项目中有很多图片icon时,效果将是M级)       3. 几乎可以忽略屏幕大小和分辨率,做到更好的适配       4. 使用简单       -- 缺点:        1. 只能是一些简单的icon,不能代替如背景图.9图

Android中正确使用字体图标(iconfont)的方法

字体图标 字体图标是指将图标做成字体文件(.ttf),从而代替传统的png等图标资源. 使用字体图标的优点和缺点分别为: 优点: 1. 可以高度自定义图标的样式(包括大小和颜色),对于个人开发者尤其适用 2. 可以减少项目和安装包的大小(特别你的项目中有很多图片icon时,效果将是M级) 3. 几乎可以忽略屏幕大小和分辨率,做到更好的适配 4. 使用简单 -- 缺点:        1. 只能是一些简单的icon,不能代替如背景图.9图等资源 2. 一些需要文字说明的icon,图片资源将会是更好

Android 中TabLayout自定义选择背景滑块的实例代码_Android

 TabLayout是Android 的Material Design包中的一个控件,可以和V4包中的ViewPager搭配产生一个联动的效果.这里我自定义了一个滑块能够跟随TabLayout进行滑动选择的SliderLayout.效果见下图(白色方框): 下面是SliderLayout的源码: import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawab

xml-如何解析 android 中的 XML?

问题描述 如何解析 android 中的 XML? 我需要把下面的 xml 解析 为输出: String id="3" String name="str1" String path="/mnt/sdcard/path2" String type="2" String desc="des3)" 在 android中如何实现呢? <xmldump> <mfs id="3"

Android中button点击后字体的变色效果_Android

button的点击效果无疑是非常简单的,以致于我懒到当UI告诉我说在点击的时候button字体的颜色也要随着背景改变的时候我毫不犹豫的告诉他让他切两个图过来,后来想想着实是不太靠谱,于是了解了一下如何添加button点击的字体颜色变化效果. 1.首先你要在你的color文件下加入几个你需要的色值,注意不同的是不是一般的color标签,而是drawable标签,就像这样: <drawable name="color_red">#fffa3d39</drawable>

Android中button点击后字体的变色效果

button的点击效果无疑是非常简单的,以致于我懒到当UI告诉我说在点击的时候button字体的颜色也要随着背景改变的时候我毫不犹豫的告诉他让他切两个图过来,后来想想着实是不太靠谱,于是了解了一下如何添加button点击的字体颜色变化效果. 1.首先你要在你的color文件下加入几个你需要的色值,注意不同的是不是一般的color标签,而是drawable标签,就像这样: <drawable name="color_red">#fffa3d39</drawable>

Android 中TabLayout自定义选择背景滑块的实例代码

TabLayout是Android 的Material Design包中的一个控件,可以和V4包中的ViewPager搭配产生一个联动的效果.这里我自定义了一个滑块能够跟随TabLayout进行滑动选择的SliderLayout.效果见下图(白色方框): 下面是SliderLayout的源码: import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawabl