SORU
9 Ocak 2012, PAZARTESİ


Ekleme özel Android Tema için Yazı tipi

Android Temalar özel yazı tipleri eklemek için herhangi bir yolu var mı?

Quick Tip: Customize Android Fonts ama burada programmetrically metin için özel yazı tipi eklemek lazım okudum.

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");  
txt.setTypeface(font); 

Ama stil/tema tarafından özel yazı tipi ayarlamak istiyorum.

CEVAP
17 NİSAN 2012, Salı


Bu this question this one kopyası olduğunu düşünüyorum.

Zamanı benim faaliyetlerinde, bu gibi bir şey kullanabilirsiniz:

FontUtils.setCustomFont(findViewById(R.id.top_view), getAssets());

XML:

        <TextView
            android:id="@ id/my_label"
            android:tag="condensed"
            android:text="@string/label"
            ... />

Yani teorik olarak stil oluşturmak ve birlikte FontUtils/çalıştırma kodu ile kullanabilirsiniz.

<style name="roboto_condensed">
    <item name="android:tag">condensed,your-own-css-like-language-here</item>
</style>

Sınıf FontUtils:

public class FontUtils {
  private static Typeface normal;

  private static Typeface bold;

  private static Typeface condensed;

  private static Typeface light;

  private static void processsViewGroup(ViewGroup v, final int len) {

    for (int i = 0; i < len; i  ) {
      final View c = v.getChildAt(i);
      if (c instanceof TextView) {
        setCustomFont((TextView) c);
      } else if (c instanceof ViewGroup) {
        setCustomFont((ViewGroup) c);
      }
    }
  }

  private static void setCustomFont(TextView c) {
    Object tag = c.getTag();
    if (tag instanceof String) {
      if (((String) tag).contains("bold")) {
        c.setTypeface(bold);
        return;
      }
      if (((String) tag).contains("condensed")) {
        c.setTypeface(condensed);
        return;
      }
      if (((String) tag).contains("light")) {
        c.setTypeface(light);
        return;
      }
    }
    c.setTypeface(normal);
  }

  public static void setCustomFont(View topView, AssetManager assetsManager) {
    if (normal == null || bold == null || condensed == null || light == null) {
      normal = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Regular.ttf");
      bold = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Bold.ttf");
      condensed = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Condensed.ttf");
      light = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Light.ttf");
    }

    if (topView instanceof ViewGroup) {
      setCustomFont((ViewGroup) topView);
    } else if (topView instanceof TextView) {
      setCustomFont((TextView) topView);
    }
  }

  private static void setCustomFont(ViewGroup v) {
    final int len = v.getChildCount();
    processsViewGroup(v, len);
  }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • gadgetgal38

    gadgetgal38

    9 HAZİRAN 2009
  • L- Crysis

    L- Crysis

    30 Aralık 2011
  • TheDigiCraft

    TheDigiCraft

    25 NİSAN 2011