標籤:
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 字型相關總結