Android實現自訂字型

來源:互聯網
上載者:User

標籤:

介紹

最近在看開源項目的時候,發現裡面涉及到了自訂字型,雖然自己目前還用不到,但是動手demo筆記記錄一下還是有必要的,沒準哪天需要到這個功能。

原理

1、其實實現起來非常簡單,主要是用到了Typeface這個類,通過載入assets裡的ttf字型,調用View.setTypeface實現原生字型替代。

預設內建樣式
public static final int NORMAL = 0;
public static final int BOLD = 1;
public static final int ITALIC = 2;
public static final int BOLD_ITALIC = 3;

預設內建Typeface

public static final Typeface DEFAULT;
public static final Typeface DEFAULT_BOLD;
public static final Typeface SANS_SERIF;
public static final Typeface SERIF;
public static final Typeface MONOSPACE;

2、通過聲明Paint來設定相關參數,調用View.getPaint().set***可以實現自訂樣式。

3、具體相關操作可以看文檔。

實現

main布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.customfont.MainActivity" >    <LinearLayout        android:id="@+id/ll_root"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello!注意我的字型喲!"            android:textAppearance="?android:attr/textAppearanceLarge" />        <EditText            android:id="@+id/editText1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:ems="10"            android:text="看我自訂字型" />        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="改變字型按鈕" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <TextView            android:id="@+id/TextView01"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="我是原生的字型"            android:textAppearance="?android:attr/textAppearanceLarge" />        <TextView            android:id="@+id/textView1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="我是原生的字型改樣式"            android:textAppearance="?android:attr/textAppearanceLarge" />        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="原生按鈕" />    </LinearLayout></LinearLayout>

核心代碼

package com.example.customfont;import android.support.v7.app.ActionBarActivity;import android.graphics.Paint;import android.graphics.Typeface;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends ActionBarActivity {    TextView textView1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView1=(TextView)findViewById(R.id.textView1);        textView1.getPaint().setFakeBoldText(true); //true為粗體,false為非粗體        textView1.getPaint().setTextSkewX(-0.5f); //float型別參數,負數表示右斜,正數左斜        textView1.getPaint().setUnderlineText(true); //true為底線,false為非底線        textView1.getPaint().setStrikeThruText(true); //true為刪除線,false為非刪除線                //從Assets擷取字型檔        Typeface mFont = Typeface.createFromAsset(getAssets(),                "STXINGKA.TTF");        //設定字型控制項        ViewGroup root = (ViewGroup) findViewById(R.id.ll_root);        setFont(root, mFont);    }    /**     * 設定ViewGroup中的指定的控制項字型為指定字型。     * @param group      * @param font     */    public void setFont(ViewGroup group, Typeface font) {        int count = group.getChildCount();        View v;        for (int i = 0; i < count; i++) {            v = group.getChildAt(i);            //判斷View的類型,只設定指定類型的字型。            if (v instanceof TextView || v instanceof EditText                    || v instanceof Button) {                ((TextView) v).getPaint().setTypeface(font);            }            //如果是ViewGroup,迭代。            else if (v instanceof ViewGroup)                setFont((ViewGroup) v, font);        }    }}

運行之後看起來是這樣的

demo地址:

連結:http://pan.baidu.com/s/1eQoyQPW 密碼:4njm

 

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.