android添加第三方字体并设置的简单使用

1、java文件


  1. package lpc.com.project006;
  2. import android.app.Activity;
  3. import android.content.res.AssetManager;
  4. import android.graphics.Typeface;
  5. import android.os.Bundle;
  6. import android.widget.EditText;
  7. /**
  8. * 此程序没有什么高神的功能,只是一个添加第三方字体,并且应用的功能
  9. *
  10. * 1、犯了一个比较二的错误,assets的位置不对,assents的正确位置是src/main 下
  11. * 2、对于字体因为太大而导致的问题,可以将字体的扩展名改成zip或者jpg就会解决、
  12. * */
  13. public class MainActivity extends Activity {
  14. private EditText et;
  15. private Typeface tf;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. et = (EditText) findViewById(R.id.et);
  21. AssetManager mgr=getAssets();//得到AssetManager
  22. tf = Typeface.createFromAsset(mgr, "fonts/1.zip");//根据路径得到Typeface
  23. et.setTypeface(tf);//设置字体
  24. }
  25. }

2、布局文件

里面只有一个简单的editview
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="lpc.com.project006.MainActivity">
  11. <EditText
  12. android:id="@+id/et"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:hint="请输入电话号码" />
  16. </RelativeLayout>

3、manfests文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="lpc.com.project006">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme">
  10. <activity android:name=".MainActivity">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. </application>
  17. </manifest>

4、字体文件




5、运行效果












posted @ 2016-01-26 15:24  liupengcheng  阅读(2253)  评论(0编辑  收藏  举报