自定義TextView顯示.ttf文件中的字符

利用Typeface顯示.ttf文件中的字符在TextView上面。java

效果圖:

TypefaceTextView

代碼以下:
/** * 做者:秦川小將 * 描述: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);
          }
      }
  }
自定義屬性:
<declare-styleable name="TypefaceTextView">
        <!--assets文件夾下的.ttf文件地址-->
        <attr name="typefacePath" format="string" />
        <!--Unicode碼-->
        <attr name="typefaceUnicode" format="reference|string" />
    </declare-styleable>
在佈局中使用:
<?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>

屬性說明:android

typefacetext:typefacePath="navigation/home_page_navigation.ttf"

navigation/home_page_navigation.ttf 爲assets文件夾下.ttf文件git

typefacetext:typefaceUnicode="@string/icon_home"

@string/icon_home 爲string中的Unicode字符碼github

案例中Unicode對應的stringweb

<resources>
    <string name="icon_home">>&#xe6a9;</string>
    <string name="icon_search">>&#xe674;</string>
    <string name="icon_message">&#xe64b;</string>
    <string name="icon_user">&#xe662;</string>
  </resources>

點擊下載案例中ttf文件app

本文分享 CSDN - 秦川小將。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。svg

相關文章
相關標籤/搜索