Android 自定义TextView显示.ttf文件中的字符

vukq9605 7年前
   <p>利用 Typeface 显示.ttf文件中的字符在TextView上面。</p>    <p>效果图:</p>    <p><img src="https://simg.open-open.com/show/c75ead2614b44d976aaca80ca4f3ab34.png"></p>    <p style="text-align:center">TypefaceTextView</p>    <p>代码如下:</p>    <pre>  <code class="language-java">/**     * 作者:僧格洛卓      * 描述:TypefaceTextView     */    public class TypefaceTextView extends TextView {          public TypefaceTextView(Context context) {            super(context);            init(context, null);        }          public TypefaceTextView(Context context, @Nullable AttributeSet attrs) {            super(context, attrs);            init(context, attrs);        }          private void init(Context context, AttributeSet attrs) {            // 自定义属性            TypedArray mArray = context.obtainStyledAttributes(attrs, R.styleable.TypefaceTextView);            String mTypefacePath = mArray.getString(R.styleable.TypefaceTextView_typefacePath);            String mTypefaceUnicode = mArray.getString(R.styleable.TypefaceTextView_typefaceUnicode);            mArray.recycle();            if (!TextUtils.isEmpty(mTypefacePath) && !TextUtils.isEmpty(mTypefaceUnicode)) {                setTypeface(Typeface.createFromAsset(context.getAssets(), mTypefacePath));                setText(mTypefaceUnicode);            }        }    }</code></pre>    <p>自定义属性:</p>    <pre>  <code class="language-java"><declare-styleable name="TypefaceTextView">          <!--assets文件夹下的.ttf文件地址-->          <attr name="typefacePath" format="string" />          <!--Unicode码-->          <attr name="typefaceUnicode" format="reference|string" />      </declare-styleable></code></pre>    <p>在布局中使用:</p>    <pre>  <code class="language-java"><?xml version="1.0" encoding="utf-8"?>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       xmlns:typefacetext="http://schemas.android.com/apk/res-auto"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:background="@color/white"       android:gravity="center"       android:orientation="horizontal">        <com.example.app.view.widget.TypefaceTextView          android:id="@+id/navigation_home"          android:layout_width="0dp"          android:layout_height="match_parent"          android:layout_weight="1"          android:gravity="center"          android:padding="5dp"          android:textColor="@color/gray"          android:textSize="25dp"          typefacetext:typefacePath="navigation/home_page_navigation.ttf"          typefacetext:typefaceUnicode="@string/icon_home" />        <com.example.app.view.widget.TypefaceTextView          android:id="@+id/navigation_search"          android:layout_width="0dp"          android:layout_height="match_parent"          android:layout_weight="1"          android:gravity="center"          android:padding="5dp"          android:textColor="@color/gray"          android:textSize="25dp"          typefacetext:typefacePath="navigation/home_page_navigation.ttf"          typefacetext:typefaceUnicode="@string/icon_search" />        <com.example.app.view.widget.TypefaceTextView          android:id="@+id/navigation_message"          android:layout_width="0dp"          android:layout_height="match_parent"          android:layout_weight="1"          android:gravity="center"          android:padding="5dp"          android:textColor="@color/gray"          android:textSize="25dp"          typefacetext:typefacePath="navigation/home_page_navigation.ttf"          typefacetext:typefaceUnicode="@string/icon_message" />        <com.example.app.view.widget.TypefaceTextView          android:id="@+id/navigation_user"          android:layout_width="0dp"          android:layout_height="match_parent"          android:layout_weight="1"          android:gravity="center"          android:textColor="@color/gray"          android:textSize="20dp"          typefacetext:typefacePath="navigation/home_page_navigation.ttf"          typefacetext:typefaceUnicode="@string/icon_user" />  </LinearLayout></code></pre>    <p>属性说明:</p>    <pre>  <code class="language-java">typefacetext:typefacePath="navigation/home_page_navigation.ttf"</code></pre>    <p>navigation/home_page_navigation.ttf 为assets文件夹下.ttf文件</p>    <pre>  <code class="language-java">typefacetext:typefaceUnicode="@string/icon_home"</code></pre>    <p>@string/icon_home 为string中的Unicode字符码</p>    <p>案例中Unicode对应的string</p>    <pre>  <code class="language-java"><resources>      <string name="icon_home"></string>      <string name="icon_search"></string>      <string name="icon_message"></string>      <string name="icon_user"></string>    </resources></code></pre>    <p> </p>    <p> </p>    <p> </p>