(三十七)Android開發中修改程式字型

來源:互聯網
上載者:User

標籤:

 1、在Android XML檔案中設定系統預設的字型
  可以在XML檔案中採用android:typeface設定字型,例如android:typeface=”monospace”。在這裡例子中我們在Activity中對android:text=”Hello, World! 您好”分別進行了四種顯示方式,依次為“Sans”,“serif”,“monospace”和系統預設方式(經實驗預設採用採用sans)。英文字型有差異,貌似中文字型沒有差異。XML檔案如下:

 

 <?xml version=“1.0″ encoding=”utf-8″?>   <TableLayout android:stretchColumns = “1”>  <TableRow>  <TextView android:text=“sans:”   android:layout_marginRight=“4px”   android:textSize=“20sp” />  <TextView android:text=”Hello, World! 您好”  android:typeface =“sans” <!– android:typeface用於指定字型–>   android:textSize=“20sp” />  </TableRow>  <TableRow>   <TextView android:text=“custom:”  />   <TextView android:id=“@+id/c12_custom”   android:text=“Hello, World! 您好”   android:textSize=“20sp” />   </TableRow>   </TableLayout>

 

  2、使用其他字型

  1)將新字型的TTF檔案copy到assets/fonts/目錄下面,例如我們將“*.ttf”copy了過去。
  2)我們需要將widget設定為該字型,比較遺憾的是,不能直接在XML檔案中進行,需要編寫原始碼。

 

 TextView tv = (TextView)findViewById(R.id.c12_custom);  //從assert中擷取有資源,獲得app的assert,採用getAserts(),通過給出在assert/下面的相對路徑。在實際使用中,字型庫可能存在於SD卡上,可以採用createFromFile()來替代createFromAsset。  Typeface face = Typeface.createFromAsset (getAssets() , “fonts/timesi.ttf” ); tv.setTypeface (face);

  在模擬器中先後匯入華文行楷的字型,大約4M,但是系統無法識別出該字型,沒有顯示,然後嘗試使用英文字型timesi.ttf,正常。因此Android並非和所有的TTF字型都能相容,尤其在中文特殊字型的支援會存在問題,對於不相容的字型,Android不出報錯,只是無法正常顯示。一般而言我們都會使用系統預設提供的體。

  對於華文行楷字型,我們一開始使用的檔案是中文名字,出現報錯,後來我們將之改為全小寫英文名稱就不會出錯,所以在檔案命名上需要注意。

3、一些注意
  使用其他字型檔,都會消耗程式的空間,這是要非常注意的。而且這些字型檔有時並不能完全提供你所需要的文字。
  舉個例子,省略方式。當文字太多的時候,可以通過省略符號省略後面的內容,省略符號是使用“…”作為一個字型,可通過android:ellipsize屬性進行設定。如果我們需要使用省略功能,需要確保字型具有省略符號。此外,為了保證長度的一直,Android會進行填充處理,除了將一個字元更換為省略符合外,後面的字元將更換為一個特殊的Unicode字元,‘ZERO WIDTH NO-BREAK SPACE’(U+FEFF)。這個字元並佔用任何可視的位置,但是保障了string具有同樣的長度。不是所有的字型都支援這個特殊的字元,可能會引發一些亂碼現象。
  Android是支援國際語言的,但是我們仍需要對custom的字型小心處理。

4、如果一個程式中多處需要修改字型,可以通過使用自訂的textView,代碼如下所示(2)所示,在裡面定義方法定義字型,以免N多地方都要使用setTypeface設定字型,非常麻煩。
使用時的代碼如表(1)所示

表(1):<com.cn21.esafe.utils.CustomFontTextView        android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_centerInParent="true"        android:gravity="center"        android:textColor="@color/white"        android:textSize="@dimen/text_title" />    
表(2):package com.cn21.esafe.utils;import android.content.Context;import android.graphics.Typeface;import android.util.AttributeSet;import android.widget.TextView;public class CustomFontTextView extends TextView {    private static Typeface typeface;    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        setCustomTypeface(context);    }    public CustomFontTextView(Context context, AttributeSet attrs) {        super(context, attrs);        setCustomTypeface(context);    }    public CustomFontTextView(Context context) {        super(context);        setCustomTypeface(context);    }    private void setCustomTypeface(Context context) {        if (typeface == null) {            typeface = Typeface.createFromAsset(context.getAssets(),                    "fonts/fangzhenglanting.ttf");        }        setTypeface(typeface);    }    public static Typeface getTypeface(Context context) {        if (typeface == null) {            typeface = Typeface.createFromAsset(context.getAssets(),                    "fonts/fangzhenglanting.ttf");        }        return typeface;    }}

 

(三十七)Android開發中修改程式字型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.