Android中字体的处理
最新推荐文章于 2024-04-16 08:03:01 发布

wonderful19891024 于 2010-11-24 21:23:00 发布
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
1、Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace
2、在Android中可以引入其他字体
3、示例如下:
4、布局文件
main.xml
<?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:text="sans:"
android:layout_marginRight="4px"
android:textSize="20sp"></TextView>
<!-- 使用默认的sans字体-->
<TextView android:id="@+id/sans"
android:text="Hello,World"
android:typeface="sans"
android:textSize="20sp"></TextView>
</TableRow>
<TableRow>
<TextView android:text="serif:"
android:layout_marginRight="4px"
android:textSize="20sp"></TextView>
<!-- 使用默认的serifs字体-->
<TextView android:id="@+id/serif"
android:text="Hello,World"
android:typeface="serif"
android:textSize="20sp"></TextView>
</TableRow>
<TableRow>
<TextView android:text="monospace:"
android:layout_marginRight="4px"
android:textSize="20sp"></TextView>
<!-- 使用默认的monospace字体-->
<TextView android:id="@+id/monospace"
android:text="Hello,World"
android:typeface="monospace"
android:textSize="20sp"></TextView>
</TableRow>
<!-- 这里没有设定字体,我们将在Java代码中设定-->
<TableRow>
<TextView android:text="custom:"
android:layout_marginRight="4px"
android:textSize="20sp"></TextView>
<TextView android:id="@+id/custom"
android:text="Hello,World"
android:textSize="20sp"></TextView>
</TableRow>
</TableLayout>
5、Java代码
package yyl.fonts;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class FontsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//得到TextView控件对象
TextView textView = (TextView)findViewById(R.id.custom);
//将字体文件保存在assets/fonts/目录下,创建Typeface对象
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
//应用字体
textView.setTypeface(typeFace);
}
}
相关知识
UI设计中关于字体规范
【Android开发那点破事】Android中Activity的生命周期
逐浪立楷字体下载 逐浪立楷字体 字体下载
android 布局中的单位及分辨率自解
Android 移动开发
基于Android的水稻病虫害图像的识别与诊断系统
Android 中 Activity的生命周期 和 Log输出
CSS3中常用字体图标库总结
Android 移动应用基础教程(Android Studio)(第2版)学习笔记及源码
【移动应用开发技术】Android中怎么保存数据.docx
原文链接: Android中字体的处理 https://m.huajiangbk.com/newsview661694.html
| 上一篇: 华南国际印刷展:2024印刷行业... | 下一篇: 本溪市公安局北地派出所积极开展印... |



