Android(安卓)引用外部字体
16lz
2021-12-04
在Android中,加载外部字体是非常容易的!
步骤如下:
1. 创建新的Android工程;
2. 在工程下的assets文件夹下新建名字为fonts的文件夹(名字可以任意选取),把所有的外部字体文件都放在这里面;如下图所示:
3. 编辑main.xml,代码如下:
main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#222222" > <TextView android:id="@+id/ghost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="70dip" android:gravity="center" android:textColor="#ef0000" android:layout_marginTop="50dip" android:text="ghost" /> </LinearLayout>
5.编辑Activity代码,代码如下:
AndroidExternalFontsActivity.javapackage com.example.androidhive; import android.app.Activity;import android.graphics.Typeface;import android.os.Bundle;import android.widget.TextView; public class AndroidExternalFontsActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Font path String fontPath = "fonts/Face Your Fears.ttf"; // text view label TextView txtGhost = (TextView) findViewById(R.id.ghost); // Loading Font Face Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); // Applying font txtGhost.setTypeface(tf); }}
6.效果图:
更多相关文章
- Android(安卓)利用JNI调用Android(安卓)Java代码函数
- Android高手进阶教程(七)之----Android(安卓)中Preferences的使
- Android浮动窗口实现原理及代码实例
- Android(安卓)的实现TextView中文字链接的4种方法
- Android(安卓)- 文件读写操作 总结
- 获取Android的Java源代码并在Eclipse中关联查看的最新方法
- 访问Android内部RIL接口(一)
- Android与HTML+JS交互入门
- android app请求获取root权限