(三十七)Android開發(fā)中修改程序字體-創(chuàng)新互聯(lián)

1、在Android XML文件中設(shè)置系統(tǒng)默認(rèn)的字體
  可以在XML文件中采用android:typeface設(shè)置字體,例如android:typeface=”monospace”。在這里例子中我們?cè)贏ctivity中對(duì)android:text=”Hello, World! 您好”分別進(jìn)行了四種顯示方式,依次為“Sans”,“serif”,“monospace”和系統(tǒng)缺省方式(經(jīng)試驗(yàn)缺省采用采用sans)。英文字體有差異,貌似中文字體沒有差異。XML文件如下:

創(chuàng)新互聯(lián)公司于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元景東做網(wǎng)站,已為上家服務(wù),為景東各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
 <?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、使用其他字體(三十七)Android開發(fā)中修
改程序字體

1)將新字體的TTF文件copy到assets/fonts/目錄下面,例如我們將“*.ttf”copy了過去。
  2)我們需要將widget設(shè)置為該字體,比較遺憾的是,不能直接在XML文件中進(jìn)行,需要編寫源代碼。

 TextView tv = (TextView)findViewById(R.id.c12_custom);
  //從assert中獲取有資源,獲得app的assert,采用getAserts(),通過給出在assert/下面的相對(duì)路徑。在實(shí)際使用中,字體庫(kù)可能存在于SD卡上,可以采用createFromFile()來替代createFromAsset?!ypeface face = Typeface.createFromAsset (getAssets() , “fonts/timesi.ttf” );
 tv.setTypeface (face);

  在模擬器中先后導(dǎo)入華文行楷的字體,大約4M,但是系統(tǒng)無法識(shí)別出該字體,沒有顯示,然后嘗試使用英文字體timesi.ttf,正常。因此Android并非和所有的TTF字體都能兼容,尤其在中文特殊字體的支持會(huì)存在問題,對(duì)于不兼容的字體,Android不出報(bào)錯(cuò),只是無法正常顯示。一般而言我們都會(huì)使用系統(tǒng)缺省提供的體。

  對(duì)于華文行楷字體,我們一開始使用的文件是中文名字,出現(xiàn)報(bào)錯(cuò),后來我們將之改為全小寫的英文名稱就不會(huì)出錯(cuò),所以在文件命名上需要注意。

3、一些注意
  使用其他字庫(kù),都會(huì)消耗程序的空間,這是要非常注意的。而且這些字庫(kù)有時(shí)并不能完全提供你所需要的文字。
  舉個(gè)例子,省略方式。當(dāng)文字太多的時(shí)候,可以通過省略號(hào)省略后面的內(nèi)容,省略號(hào)是使用“…”作為一個(gè)字體,可通過android:ellipsize屬性進(jìn)行設(shè)置。如果我們需要使用省略功能,需要確保字體具有省略號(hào)。此外,為了保證長(zhǎng)度的一直,Android會(huì)進(jìn)行填充處理,除了將一個(gè)字符更換為省略符合外,后面的字符將更換為一個(gè)特殊的Unicode字符,‘ZERO WIDTH NO-BREAK SPACE’ (U+FEFF)。這個(gè)字符并占用任何可視的位置,但是保障了string具有同樣的長(zhǎng)度。不是所有的字體都支持這個(gè)特殊的字符,可能會(huì)引發(fā)一些亂碼現(xiàn)象。
  Android是支持國(guó)際語(yǔ)言的,但是我們?nèi)孕枰獙?duì)custom的字體小心處理。

4、如果一個(gè)程序中多處需要修改字體,可以通過使用自定義的textView,代碼如下所示(2)所示,在里面定義方法定義字體,以免N多地方都要使用setTypeface設(shè)置字體,非常麻煩。
使用時(shí)的代碼如表(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;
    }
}

當(dāng)前文章:(三十七)Android開發(fā)中修改程序字體-創(chuàng)新互聯(lián)
當(dāng)前地址:http://muchs.cn/article4/cdccie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站小程序開發(fā)、網(wǎng)站營(yíng)銷、服務(wù)器托管軟件開發(fā)、營(yíng)銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站