Android中通過typeface設定字型

來源:互聯網
上載者:User

標籤:

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);

Android中通過typeface設定字型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.