Android中通過typeface設置字體


Android系統默認支持三種字體,分別為:sans, serif, monospace",除此之外還可以使用其他字體文件(*.ttf)

方法一:XML中使用android默認字體

<!--  使用默認的sans字體-->
        <TextView    Android:id="@+id/sans"
                   Android:text="Hello,World"
                   Android:typeface="sans"
                   Android:textSize="20sp" />

<!--  使用默認的serifs字體-->
        <TextView   Android:id="@+id/serif"
                   Android:text="Hello,World"
                   Android:typeface="serif"
                   Android:textSize="20sp" />

<!--  使用默認的monospace字體-->
        <TextView   Android:id="@+id/monospace"
                   Android:text="Hello,World"
                   Android:typeface="monospace"
                   Android:textSize="20sp" />

方法Android中可以引入其他字體,首先要將字體文件保存在assets/fonts/目錄下

1.  <!--  這里沒有設定字體,將在Java代碼中設定-->

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

2.  java程序中引入其他字體關鍵代碼

  //得到TextView控件對象
        TextView textView =(TextView)findViewById(R.id.custom);

  //將字體文件保存在assets/fonts/目錄下,創建Typeface對象

  Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf");

  //使用字體

  textView.setTypeface(typeFace);

  
      


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM