2012-03-21 14 views
20

मैं अपने एंड्रॉइड एप्लिकेशन में रोबोटो फ़ॉन्ट का उपयोग करना चाहता हूं और यह सुनिश्चित करता हूं कि यह एंड्रॉइड के पुराने संस्करणों के लिए काम करता है जिसमें फ़ॉन्ट स्थापित नहीं है। मुझे पता है कि मैं Typeface.createFromAsset() का उपयोग कर ऐसा कर सकता हूं और फिर मैन्युअल रूप से मेरे प्रत्येक टेक्स्ट व्यू/बटन/अन्य ऑब्जेक्ट्स के लिए फ़ॉन्ट सेट कर सकता हूं। हालांकि स्क्रीन पर दिखाए गए हर ऑब्जेक्ट के लिए ऐसा करने के लिए यह एक बड़ा दर्द लगता है।पुराने उपकरणों के लिए रोबोटो फ़ॉन्ट का उपयोग करें

मेरा सवाल है, क्या ऐसा करने का एक बेहतर तरीका है? कुछ सहायक वर्ग या एक .xml थीम फ़ाइल में एक कस्टम फ़ॉन्ट सेट करने का एक तरीका? स्वचालित रूप से कुछ भी प्रत्येक स्क्रीन पर प्रत्येक ऑब्जेक्ट को मैन्युअल रूप से सूचीबद्ध करने और फ़ॉन्ट को बदलने से बेहतर होगा।

धन्यवाद!

+0

है आप में एक [शैली] यह डाल सकते हैं (http://developer.android.com/guide/topics/ui/themes.html) हो सकता है। आईडीक अगर यह संपत्ति से टाइपफेस के साथ काम करता है। – zapl

उत्तर

16

गतिविधि के अंदर सभी विचारों को पुनर्प्राप्त करें, इसके प्रकार की जांच करें और उचित कार्रवाई करें।

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf"); 
for (View view : allViews) 
{ 
if (view instanceof TextView) 
{ 
    TextView textView = (TextView) view; 
    textView.setTypeface(typeface); 
    } 
} 
30

स्वीकार किए जाते हैं जवाब से ऊपर सही है, लेकिन मैं सिर्फ अपनी कार्यान्वयन यहाँ की आपूर्ति करना चाहता था

मेरे उपयोगिता वर्ग:

:

package com.example.utils; 

import android.content.Context; 
import android.graphics.Typeface; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class AndroidUtils 
{ 
    private static Typeface robotoTypeFace; 

    public static void setRobotoFont (Context context, View view) 
    { 
     if (robotoTypeFace == null) 
     { 
      robotoTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto/Roboto-Regular.ttf"); 
     } 
     setFont(view, robotoTypeFace); 
    } 

    private static void setFont (View view, Typeface robotoTypeFace) 
    { 
     if (view instanceof ViewGroup) 
     { 
      for (int i = 0; i < ((ViewGroup)view).getChildCount(); i++) 
      { 
       setFont(((ViewGroup)view).getChildAt(i), robotoTypeFace); 
      } 
     } 
     else if (view instanceof TextView) 
     { 
      ((TextView) view).setTypeface(robotoTypeFace); 
     } 
    } 
} 

इसका उपयोग कैसे करें, this संभालने एक गतिविधि है

AndroidUtils.setRobotoFont(this, view); 

उसी टेक्स्ट को सभी टेक्स्ट व्यू पर सेट करने के लिए आप डी का उपयोग कर सकते हैं आपकी गतिविधि का corView:

ViewGroup godfatherView = (ViewGroup)this.getWindow().getDecorView(); 
AndroidUtils.setRobotoFont(this, godfatherView); 

यदि आपके पास एडाप्टर या टुकड़े हैं, तो अपने फ़ॉन्ट को भी सेट करना न भूलें।

here भी देखें।

3

यहां इस समाधान का मेरा बेहतर संस्करण है। यह फोंट को कैश कर सकता है और TextView.textStyle पैरामीटर सेटिंग्स को ध्यान में रखता है। तो यह बोल्ड टेक्स्ट सेट कर सकते हैं। http://anton.averin.pro/2012/09/12/how-to-use-android-roboto-font-in-honeycomb-and-earlier-versions/

+0

बहुत अच्छा! << ViewGroup godfatherView = (ViewGroup) के साथ इसका उपयोग करना .getWindow()। GetDecorView(); getDecorView(); AndroidUtils.setRobotoFont (यह, godfatherView); >> सही है। –

7

@Jitsu, @Arnaud और @Pawan एम के लिए धन्यवाद, मैं अपने समाधान, बनाया अकेले उन सभी की तुलना में बेहतर:

/** 
* Adapted from http://stackoverflow.com/a/12387343/450148 
* 
* @author Anton Averin 
* @author Felipe Micaroni Lalli 
*/ 

package net.alouw.alouwCheckin.util; 

import android.content.Context; 
import android.graphics.Typeface; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import java.util.EnumMap; 
import java.util.Map; 

public final class FontUtils { 
    private FontUtils() { 
    } 

    private enum FontType { 
     BOLD("fonts/Roboto/Roboto-BoldCondensed.ttf"), 
     BOLD_ITALIC("fonts/Roboto/Roboto-BoldCondensedItalic.ttf"), 
     NORMAL("fonts/Roboto/Roboto-Condensed.ttf"), 
     ITALIC("fonts/Roboto/Roboto-CondensedItalic.ttf"); 

     private final String path; 

     FontType(String path) { 
      this.path = path; 
     } 

     public String getPath() { 
      return path; 
     } 
    } 

    /* cache for loaded Roboto typefaces*/ 
    private static Map<FontType, Typeface> typefaceCache = new EnumMap<FontType, Typeface>(FontType.class); 

    /** 
    * Creates Roboto typeface and puts it into cache 
    */ 
    private static Typeface getRobotoTypeface(Context context, FontType fontType) { 
     String fontPath = fontType.getPath(); 

     if (!typefaceCache.containsKey(fontType)) { 
      typefaceCache.put(fontType, Typeface.createFromAsset(context.getAssets(), fontPath)); 
     } 

     return typefaceCache.get(fontType); 
    } 

    /** 
    * Gets roboto typeface according to passed typeface style settings. 
    * <p/> 
    * Will get Roboto-Bold for Typeface.BOLD etc 
    */ 
    private static Typeface getRobotoTypeface(Context context, Typeface originalTypeface) { 
     FontType robotoFontType = null; 

     if (originalTypeface == null) { 
      robotoFontType = FontType.NORMAL; 
     } else { 
      int style = originalTypeface.getStyle(); 

      switch (style) { 
       case Typeface.BOLD: 
        robotoFontType = FontType.BOLD; 
        break; 

       case Typeface.BOLD_ITALIC: 
        robotoFontType = FontType.BOLD_ITALIC; 
        break; 

       case Typeface.ITALIC: 
        robotoFontType = FontType.ITALIC; 
        break; 

       case Typeface.NORMAL: 
        robotoFontType = FontType.NORMAL; 
        break; 
      } 
     } 

     return (robotoFontType == null) ? originalTypeface : getRobotoTypeface(context, robotoFontType); 
    } 

    /** 
    * Walks ViewGroups, finds TextViews and applies Typefaces taking styling in consideration 
    * 
    * @param context - to reach assets 
    * @param view - root view to apply typeface to 
    */ 
    public static void setRobotoFont(Context context, View view) { 
     if (view instanceof ViewGroup) { 
      for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 
       setRobotoFont(context, ((ViewGroup) view).getChildAt(i)); 
      } 
     } else if (view instanceof TextView) { 
      Typeface currentTypeface = ((TextView) view).getTypeface(); 
      ((TextView) view).setTypeface(getRobotoTypeface(context, currentTypeface)); 
     } 
    } 
} 

अपने onCreate मुख्य गतिविधि में आखिरी बात और:

if (Build.VERSION.SDK_INT < 11) { 
    ViewGroup godfatherView = (ViewGroup) this.getWindow().getDecorView(); 
    FontUtils.setRobotoFont(this, godfatherView); 
} 

और कस्टम के साथ मेरी सूची में ऊपर काम नहीं किया कोड देखता है, तो मैं इस करना था:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // (...) 

    View view = // build your custom view here 

    if (Build.VERSION.SDK_INT < 11) { 
     FontUtils.setRobotoFont(activity, view); 
    } 

    return view; 
} 
+0

हाय लगता है कि यह '(अगर (Build.VERSION.SDK_INT <= 11)) 'नहीं? –

+0

सुनिश्चित नहीं है। अगर मामला है तो संपादित करने के लिए स्वतंत्र महसूस करें। –

3

मैं एक और समाधान का उपयोग करता हूं। मैंने गतिविधि के लिए कस्टम LayoutInflater.Factory सेट किया है। इसलिए इसे बनाने के बाद मुझे देखने के लिए पूर्ण पहुंच है। मैं दृश्य पदानुक्रम पर पुन: प्रयास किए बिना प्रत्येक टेक्स्ट व्यू के लिए कस्टम फ़ॉन्ट इंस्टॉल कर सकता हूं। आपके सभी एप्लिकेशन में कस्टम फ़ॉन्ट का उपयोग करने के लिए आपको एक चीज करना चाहिए अपनी बेस गतिविधि में new Font(...).install() पर कॉल करें। नीचे exaple देखें।

यहाँ उपयोग के नमूने के साथ मेरी समाधान है:

import java.util.Map; 

import com.google.common.collect.Maps; 

import static com.google.common.base.Preconditions.checkNotNull; 

import android.R; 
import android.app.Activity; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Typeface; 
import android.support.v4.app.FragmentActivity; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.InflateException; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.TextView; 

/** 
* Provides an ability to apply custom font to all {@link TextView} and subclasses. 
* 
* To install custom font use method {@link #install(Activity)} in {@link Activity#onCreate(android.os.Bundle)} 
* <b>before</b> calling super.onCreate(Bundle). 
* 
* <p/>Example of usage: 
* <pre> 
* {@code 
* public class BaseActivity extends SherlockFragmentActivity { 
* 
*  protected void onCreate(Bundle state) { 
*   applyCustomFontForPreICS(); 
*   super.onCreate(state); 
*  } 
* 
*  private void applyCustomFontForPreICS() { 
*   boolean isPreICS = Build.VERSION.SDK_INT < BUILD_VERSION_CODE_ICE_CREAM_SANDWICH 
*   if (isPreICS) { 
*    new Font(
*     "font/roboto_regular.ttf", 
*     "font/roboto_bold.ttf", 
*     "font/roboto_italic.ttf", 
*     "font/roboto_bold_italic.ttf" 
*    ).install(this); 
*   } 
*  } 
* } 
* } 
* </pre> 
* 
* @author Alexey Danilov ([email protected]) 
*/ 
public class Font { 

    private static final Map<String, Typeface> FONTS = Maps.newHashMap(); 

    private String regularFontPath; 
    private String boldFontPath; 
    private String italicFontPath; 
    private String boldItalicFontPath; 

    /** 
    * Creates instance to be used for setting particular font. 
    * 
    * @param regularPath regular font assets path, must be not {@code null} 
    * @param boldPath bold font assets path, must be not {@code null} 
    * @param italicPath italic font assets path, must be not {@code null} 
    * @param boldItalicPath bold and italic font assets path, must be not {@code null} 
    */ 
    public Font(String regularPath, String boldPath, String italicPath, String boldItalicPath) { 
     this.regularFontPath = checkNotNull(regularPath); 
     this.boldFontPath = checkNotNull(boldPath); 
     this.italicFontPath = checkNotNull(italicPath); 
     this.boldItalicFontPath = checkNotNull(boldItalicPath); 
    } 

    /** 
    * Installs custom font to activity. 
    * 
    * @param activity an activity custom font will be installed to, must be not {@code null}. 
    */ 
    public void install(Activity activity) { 
     checkNotNull(activity, "Activity must be not null!"); 

     LayoutInflater layoutInflater = activity.getLayoutInflater(); 
     boolean factoryIsEmpty = layoutInflater.getFactory() == null; 
     if (!factoryIsEmpty) { 
      throw new IllegalStateException("Impossible to use this method for this activity: layout factory is set!"); 
     } 
     layoutInflater.setFactory(new FontLayoutInflaterFactory()); 
    } 

    private Typeface getFont(int type, Context context) { 
     switch (type) { 
      case Typeface.NORMAL: 
       return getFont(context, regularFontPath); 
      case Typeface.BOLD: 
       return getFont(context, boldFontPath); 
      case Typeface.ITALIC: 
       return getFont(context, italicFontPath); 
      case Typeface.BOLD_ITALIC: 
       return getFont(context, boldItalicFontPath); 
      default: { 
       throw new IllegalArgumentException("Undefined font type " + type); 
      } 
     } 
    } 

    private Typeface getFont(Context context, String path) { 
     if (FONTS.containsKey(path)) { 
      return FONTS.get(path); 
     } else { 
      Typeface typeface = makeTypeface(context, path); 
      FONTS.put(path, typeface); 
      return typeface; 
     } 
    } 

    private Typeface makeTypeface(Context context, String path) { 
     try { 
      return Typeface.createFromAsset(context.getAssets(), path); 
     } catch (Exception e) { 
      // add user-friendly error message 
      throw new IllegalArgumentException(String.format("Error creating font from assets path '%s'", path), e); 
     } 
    } 

    private void applyFontToTextView(Context context, TextView textView, AttributeSet attrs) { 
     int[] fontStyleAttributes = {R.attr.textStyle}; 
     TypedArray typedArray = context.obtainStyledAttributes(attrs, fontStyleAttributes); 
     boolean isStyleSpecified = typedArray.getIndexCount() != 0; 
     int type = isStyleSpecified ? typedArray.getInt(0, Typeface.NORMAL) : Typeface.NORMAL; 
     Typeface font = getFont(type, context); 
     textView.setTypeface(font, type); 
    } 

    private final class FontLayoutInflaterFactory implements LayoutInflater.Factory { 

     // to improve perfomance the package with the most usable components should be the first. 
     private final String[] ANDROID_UI_COMPONENT_PACKAGES = { 
       "android.widget.", 
       "android.webkit.", 
       "android.view." 
     }; 

     @Override 
     public View onCreateView(String name, Context context, AttributeSet attrs) { 
      try { 
       // we install custom LayoutInflater.Factory, so FragmentActivity have no chance set own factory and 
       // inflate tag <fragment> in method onCreateView. So call it explicitly. 
       if ("fragment".equals(name) && context instanceof FragmentActivity) { 
        FragmentActivity fragmentActivity = (FragmentActivity) context; 
        return fragmentActivity.onCreateView(name, context, attrs); 
       } 

       View view = createView(name, attrs, LayoutInflater.from(context)); 
       if (view == null) { 
        // It's strange! The view is not ours neither android's. May be the package of this view 
        // is not listed in ANDROID_UI_COMPONENT_PACKAGES. Return null for the default behavior. 
        Log.d(LOG_TAG, "Cannot create view with name: " + name); 
        return null; 
       } 

       if (view instanceof TextView) { 
        TextView textView = (TextView) view; 
        applyFontToTextView(context, textView, attrs); 
       } 
       return view; 
      } catch (InflateException e) { 
       Log.e(LOG_TAG, "Error inflating view", e); 
       return null; 
      } catch (ClassNotFoundException e) { 
       Log.e(LOG_TAG, "Error inflating view", e); 
       return null; 
      } 
     } 

     private View createView(String name, AttributeSet attrs, LayoutInflater layoutInflater) throws ClassNotFoundException { 
      View view = null; 
      boolean isAndroidComponent = name.indexOf('.') == -1; 
      if (isAndroidComponent) { 
       // We don't know package name of the view with the given simple name. Try android ui packages listed in 
       // ANDROID_UI_COMPONENT_PACKAGES 

       // The same implementation is in the class PhoneLayoutInflater from internal API 
       for (String androidPackage : ANDROID_UI_COMPONENT_PACKAGES) { 
        try { 
         view = layoutInflater.createView(name, androidPackage, attrs); 
         if (view != null) { 
          break; 
         } 
        } catch (ClassNotFoundException e) { 
         // Do nothing, we will try another package 
        } 
       } 
      } else { 
       view = layoutInflater.createView(name, null, attrs); 
      } 
      return view; 
     } 
    } 
} 

नोट यह guava पर निर्भरता है, लेकिन आप खुद से यह तरीकों को लागू कर सकते हैं।

संबंधित मुद्दे