2017-05-04 4 views
0

Imは現在アンドロイドスタジオ2.3.1を使用しています。XMLでfontfamilyを使用してカスタムフォントを設定する方法Api 16

プログラミングではなくxmlからテキストビューにフォントを設定したいと考えています。

資産フォルダにフォントがあります。

どのように私の資産フォルダから自分のフォントにアクセスできますか? Android Oには "font"というリソースフォルダが付いてくるのがわかりました。

答えて

0

以下に示すように、TypeFactory.javaクラスを作成します。

public class TypeFactory { 

    private String A_BOLD= "Amble-Bold.ttf"; 
    private String A_LIGHT="Amble-Light.ttf"; 
    private String A_REGULAR= "Amble-Regular.ttf"; 
    private String O_ITALIC= "OpenSans-Italic.ttf"; 
    private String O_REGULAR="OpenSans-Regular.ttf"; 

    Typeface ambleBold; 
    Typeface ambleLight; 
    Typeface ambleRegular; 
    Typeface openSansItalic; 
    Typeface openSansRegular; 

    public TypeFactory(Context context){ 
     ambleBold = Typeface.createFromAsset(context.getAssets(),A_BOLD); 
     ambleLight = Typeface.createFromAsset(context.getAssets(),A_LIGHT); 
     ambleRegular = Typeface.createFromAsset(context.getAssets(),A_REGULAR); 
     openSansItalic = Typeface.createFromAsset(context.getAssets(),O_ITALIC); 
     openSansRegular = Typeface.createFromAsset(context.getAssets(),O_REGULAR); 
    } 

} 

CustomTextView.javaクラスを以下に示します。

public class CustomTextView extends TextView { 

    private int typefaceType; 
    private TypeFactory mFontFactory; 

    public CustomTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     applyCustomFont(context, attrs); 
    } 

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     applyCustomFont(context, attrs); 
    } 

    public CustomTextView(Context context) { 
     super(context); 
    } 

    private void applyCustomFont(Context context, AttributeSet attrs) { 


     TypedArray array = context.getTheme().obtainStyledAttributes(
       attrs, 
       R.styleable.CustomTextView, 
       0, 0); 
     try { 
      typefaceType = array.getInteger(R.styleable.CustomTextView_font_name, 0); 
     } finally { 
      array.recycle(); 
     } 
     if (!isInEditMode()) { 
      setTypeface(getTypeFace(typefaceType)); 
     } 

    } 

    public Typeface getTypeFace(int type) { 
     if (mFontFactory == null) 
      mFontFactory = new TypeFactory(getContext()); 

     switch (type) { 
      case Constants.A_BOLD: 
       return mFontFactory.ambleBold; 

      case Constants.A_LIGHT: 
       return mFontFactory.ambleLight; 

      case Constants.A_REGULAR: 
       return mFontFactory.ambleRegular; 

      case Constants.O_LIGHT: 
       return mFontFactory.openSansItalic; 

      case Constants.O_REGULAR: 
       return mFontFactory.openSansRegular; 

      default: 
       return mFontFactory.ambleBold; 
     } 
    } 

    public interface Constants { 
     int A_BOLD = 1, 
       A_LIGHT = 2, 
       A_REGULAR = 3, 
       O_LIGHT = 4, 
     O_REGULAR=5; 
    } 


} 

R.styleable.CustomTextViewは、以下に示すようにattrs.xmlファイル内で定義されています。 https://github.com/chrisjenx/Calligraphy

:あなたも書道を使用することができます

<?xml version="1.0" encoding="utf-8"?> 
    <resources> 

     <declare-styleable name="CustomTextView"> 
      <attr name="font_name"> 
       <enum value="1" name="ambleBold"/> 
       <enum value="2" name="ambleLight"/> 
       <enum value="3" name="ambleRegular"/> 
       <enum value="4" name="openSansItalic"/> 
       <enum value="5" name="openSansRegular"/> 
      </attr> 
     </declare-styleable> 

    </resources> 

Our updated activity_custom_fonts.xml layout is given below 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context="com.journaldev.customfonts.MainActivity"> 

    <com.journaldev.customfonts.CustomTextView 
     app:font_name="ambleBold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Amble Bold" 
     android:textSize="28sp" 
     android:id="@+id/ambleBold" 
     /> 

    <com.journaldev.customfonts.CustomTextView 
     app:font_name="ambleLight" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Amble Light" 
     android:textSize="28sp" 
     android:id="@+id/ambleLight" 
     /> 

    <com.journaldev.customfonts.CustomTextView 
     app:font_name="ambleRegular" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Amble Regular" 
     android:textSize="28sp" 
     android:id="@+id/ambleRegular" /> 

    <com.journaldev.customfonts.CustomTextView 
     app:font_name="openSansRegular" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="OpenSans Regular" 
     android:textSize="28sp" 
     android:id="@+id/opRegular" /> 

    <com.journaldev.customfonts.CustomTextView 
     app:font_name="openSansItalic" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="OpenSans-Italic" 
     android:id="@+id/opItalic" 
     android:textSize="28sp" 
     /> 

    <Button android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:text="Pacifico" 
     android:id="@+id/pacifico"/> 

</LinearLayout> 

関連する問題