Android 自訂字型

來源:互聯網
上載者:User

在android系統中,內建的只有三種字型---"sans","serif"和"monospace",你只要在xml檔案中的

android:typeface 

android:typeface屬性使用它們:

<TableLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:stretchColumns="1">  
    <TableRow>  
        <TextView  
            android:text="sans:"  
            android:layout_marginRight="4px"  
            android:textSize="20sp"  
        />  
        <TextView  
            android:id="@+id/sans"  
            android:text="Hello, world!"  
            android:typeface="sans"  
            android:textSize="20sp"  
        />  
    </TableRow>  
    <TableRow>  
        <TextView  
            android:text="serif:"  
            android:layout_marginRight="4px"  
            android:textSize="20sp"  
        />  
        <TextView  
            android:id="@+id/serif"  
            android:text="Hello, world!"  
            android:typeface="serif"  
            android:textSize="20sp"  
        />  
    </TableRow>  
    <TableRow>  
        <TextView  
            android:text="monospace:"  
            android:layout_marginRight="4px"  
            android:textSize="20sp"  
        />  
        <TextView  
            android:id="@+id/monospace"  
            android:text="Hello, world!"  
            android:typeface="monospace"  
            android:textSize="20sp"  
        />  
    </TableRow>  
    <TableRow>  
        <TextView  
            android:text="Custom:"  
            android:layout_marginRight="4px"  
            android:textSize="20sp"  
        />  
        <TextView  
            android:id="@+id/custom"  
            android:text="Hello, world!"  
            android:textSize="20sp"  
        />  
    </TableRow>  
</TableLayout> 

<TableLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:stretchColumns="1">
 <TableRow>
  <TextView
   android:text="sans:"
   android:layout_marginRight="4px"
   android:textSize="20sp"
  />
  <TextView
   android:id="@+id/sans"
   android:text="Hello, world!"
   android:typeface="sans"
   android:textSize="20sp"
  />
 </TableRow>
 <TableRow>
  <TextView
   android:text="serif:"
   android:layout_marginRight="4px"
   android:textSize="20sp"
  />
  <TextView
   android:id="@+id/serif"
   android:text="Hello, world!"
   android:typeface="serif"
   android:textSize="20sp"
  />
 </TableRow>
 <TableRow>
  <TextView
   android:text="monospace:"
   android:layout_marginRight="4px"
   android:textSize="20sp"
  />
  <TextView
   android:id="@+id/monospace"
   android:text="Hello, world!"
   android:typeface="monospace"
   android:textSize="20sp"
  />
 </TableRow>
 <TableRow>
  <TextView
   android:text="Custom:"
   android:layout_marginRight="4px"
   android:textSize="20sp"
  />
  <TextView
   android:id="@+id/custom"
   android:text="Hello, world!"
   android:textSize="20sp"
  />
 </TableRow>
</TableLayout>

    有時候,系統內建的字型並不能滿足我們特殊的需求,這時候就需要引用其他的字型了,可以把下載的字型檔放在assets目錄下.自訂字型檔不能使用xml代碼讀取而應該使用java代碼:


public class Test extends Activity {   
    @Override  
    public void onCreate(Bundle icicle) {   
        super.onCreate(icicle);   
        setContentView(R.layout.main);   
           
        TextView tv=(TextView)findViewById(R.id.custom);   
        Typeface face=Typeface.createFromAsset(getAssets(),   
                                                                                        "fonts/HandmadeTypewriter.ttf");//也可以使用其他的靜態方法擷取   
           
        tv.setTypeface(face);   
    }   

public class Test extends Activity {
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);
  
  TextView tv=(TextView)findViewById(R.id.custom);
  Typeface face=Typeface.createFromAsset(getAssets(),
                      "fonts/HandmadeTypewriter.ttf");//也可以使用其他的靜態方法擷取
  
  tv.setTypeface(face);
 }
}

TTF檔案下載:Windows.zip | MAC.zip(選擇對應的系統)

  注意:如果你下載的字型Android不支援,那麼系統會使用預設字型"sans"。由於應用自訂字型需要額外空間,所以一般不是都需要自訂字型的.

轉載:http://www.androidmi.com/Androidkaifa/jinjie/201011/1632.html

相關文章

聯繫我們

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