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

vukq9605 7年前發布 | 15K 次閱讀 TextView Android開發 移動開發

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

效果圖:

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>

屬性說明:

typefacetext:typefacePath="navigation/home_page_navigation.ttf"

navigation/home_page_navigation.ttf 為assets文件夾下.ttf文件

typefacetext:typefaceUnicode="@string/icon_home"

@string/icon_home 為string中的Unicode字符碼

案例中Unicode對應的string

<resources>
    <string name="icon_home"></string>
    <string name="icon_search"></string>
    <string name="icon_message"></string>
    <string name="icon_user"></string>
  </resources>

 

 

 

 本文由用戶 vukq9605 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!