閱讀282 返回首頁    go 阿裏雲 go 技術社區[雲棲]


ttf設置文字字體

MainActivity如下:

  1. package cn.testfont;  
  2.   
  3. import android.os.Bundle;  
  4. import android.widget.TextView;  
  5. import android.app.Activity;  
  6. import android.graphics.Typeface;  
  7. /** 
  8.  * Demo描述: 
  9.  * 利用TTF字體文件文字的顯示效果 
  10.  *  
  11.  * 步驟如下: 
  12.  * 1 在asset下建立fonts文件夾 
  13.  * 2 將.ttf文件拖入fonts文件夾Typeface 
  14.  * 3 在代碼中為TextView設置 
  15.  * 
  16.  */  
  17. public class MainActivity extends Activity {  
  18.     private TextView mTextView;  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.         init();  
  24.     }  
  25.   
  26.     private void init(){  
  27.         mTextView=(TextView) findViewById(R.id.textView);  
  28.         Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/test.ttf");  
  29.         mTextView.setTypeface(typeface);  
  30.     }  
  31. }  

main.xml如下:
  1. <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="https://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="9527"  
  12.         android:textSize="22sp"  
  13.         android:layout_centerInParent="true"  
  14.      />  
  15.   
  16. </RelativeLayout> 

最後更新:2017-04-03 07:57:03

  上一篇:go 【理想流】不要做虛情假意的管理 .
  下一篇:go 用 JavaScript 檢測 CPU 占比和內存泄露