Android 字型相關總結

來源:互聯網
上載者:User

標籤:

1、Android系統預設支援三種字型,分別為:“sans”, “serif”,  “monospace“  系統預設方式(經實驗預設採用採用sans);

2、在Android中可以引入其他字型

3、樣本如下:

4、布局檔案

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout    xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
    <TableRow>
        <TextView    android:text="sans:"
                    android:layout_marginRight="4px"
                    android:textSize="20sp"></TextView>

         <!--  使用預設的sans字型--> 
        <TextView    android:id="@+id/sans"
                    android:text="Hello,World"
                    android:typeface="sans" 
                    android:textSize="20sp"></TextView>
    </TableRow>        
    <TableRow>
        <TextView    android:text="serif:"
                    android:layout_marginRight="4px"
                    android:textSize="20sp"></TextView>

         <!--  使用預設的serifs字型--> 
        <TextView    android:id="@+id/serif"
                    android:text="Hello,World"
                    android:typeface="serif" 
                    android:textSize="20sp"></TextView>
    </TableRow>        
    <TableRow>
        <TextView    android:text="monospace:"
                    android:layout_marginRight="4px"
                    android:textSize="20sp"></TextView>

          <!--  使用預設的monospace字型--> 
        <TextView    android:id="@+id/monospace"
                    android:text="Hello,World"
                    android:typeface="monospace" 
                    android:textSize="20sp"></TextView>
    </TableRow>  

  <!--  這裡沒有設定字型,我們將在Java代碼中設定-->       
    <TableRow>
        <TextView    android:text="custom:"
                    android:layout_marginRight="4px"
                    android:textSize="20sp"></TextView>
        <TextView    android:id="@+id/custom"
                    android:text="Hello,World"
                    android:textSize="20sp"></TextView>
    </TableRow>                
</TableLayout>

 

5、Java代碼

package yyl.fonts;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class FontsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //得到TextView控制項對象
        TextView textView = (TextView)findViewById(R.id.custom);

        //將字型檔儲存在assets/fonts/目錄下,建立Typeface對象 
        Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");

        //應用字型
        textView.setTypeface(typeFace); 
    }
}

 

android TextView設定中文字型加粗實現方法

英文設定加粗可以在xml裡面設定: 

代碼如下:
<SPAN style="FONT-SIZE: 18px">android:textStyle="bold"</SPAN> 


英文還可以直接在String檔案裡面直接這樣填寫: 

代碼如下:
<string name="styled_text">Plain, <b>bold</b>, <i>italic</i>, <b><i>bold-italic</i></b></string> 


b代碼加粗,i代表傾斜 
中文設定加粗就需要在代碼中擷取到當前TextView在進行設定: 

代碼如下:
TextView tv = (TextView)findViewById(R.id.tv); 
TextPaint tp = tv.getPaint(); 
tp.setFakeBoldText(true); 

 

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.