SORU
9 Aralık 2010, PERŞEMBE


Android: tüm uygulama için özel yazı tiplerini zamanı ayarlamak İster

Uygulama her kontrol herhangi bir özel yazı tipi ayarlamak mümkün mü? Ve mutlaka zamanı değil mi ? (yani mümkün ya da sadece bir kez eğer xml JAVA dosyasında tüm uygulama için)

Bu kodu bir yazı tipini ayarlayabilirsiniz.

public static void setFont(TextView textView) {
    Typeface tf = Typeface.createFromAsset(textView.getContext()
            .getAssets(), "fonts/BPreplay.otf");

    textView.setTypeface(tf);

}

Ve bu kod ile sorun her kontrol için çağrılmalıdır. Ve bu aramak istiyorum ya da benzer herhangi bir yöntem mümkünse bir kez, ya da xml özelliğini ayarlayın. Mümkün mü?

CEVAP
9 Aralık 2010, PERŞEMBE


EDİTBir süre geçti, ve bunu yapmanın en iyi yolu, ve XML aracılığıyla bence ne daha eklemek istiyorum!

Bunun için ilk önce, özelleştirmek istediğiniz her şeyi geçersiz kılacak yeni bir sınıf yapmak istiyorum gidiyoruz. (örneğin, özel bir yazı ile bir Düğme ister misin? ** 12) uzatın. Bir örnek yapalım:

public class CustomButton extends Button {
    private final static int ROBOTO = 0;
    private final static int ROBOTO_CONDENSED = 1;

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

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        parseAttributes(context, attrs); //I'll explain this method later
    }

    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        parseAttributes(context, attrs);
    }
}

Eğer gerek olsaydı zaten şimdi res/values/attrs.xml altında bir XML belge Ekle:

<resources>
    <!-- Define the values for the attribute -->
    <attr name="typeface" format="enum">
        <enum name="roboto" value="0"/>
        <enum name="robotoCondensed" value="1"/>
    </attr>

    <!-- Tell Android that the class "CustomButton" can be styled, 
         and which attributes it supports -->
    <declare-styleable name="CustomButton">
        <attr name="typeface"/>
    </declare-styleable>
</resources>

Yoldan önceki parseAttributes() yöntemi dönelim uygun mu, yani:

private void parseAttributes(Context context, AttributeSet attrs) {
    TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.CustomButton);

    //The value 0 is a default, but shouldn't ever be used since the attr is an enum
    int typeface = values.getInt(R.styleable.CustomButton_typeface, 0);

    switch(typeface) {
        case ROBOTO: default:
            //You can instantiate your typeface anywhere, I would suggest as a 
            //singleton somewhere to avoid unnecessary copies
            setTypeface(roboto); 
            break;
        case ROBOTO_CONDENSED:
            setTypeface(robotoCondensed);
            break;
    }

    values.recycle();
}

Şimdi hazırsın. Hakkında hiçbir şey (typefaceStyle -- kalın, italik, vb için bir tane daha ekleyebilirsiniz.) için daha fazla özellik ekleyebilirsiniz ama şimdi bunu kullanmak için nasıl

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.yourpackage.name"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.yourpackage.name.CustomButton
        android:id="@ id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me!"
        custom:typeface="roboto" />

</LinearLayout>

xmlns:custom çizgi gerçekten herhangi bir şey olabilir, ancak bu Sözleşme, yukarıda gösterilen. Önemli olan benzersiz olduğunu, ve o paketi adı kullanılır. Şimdi sadece özellikleri için custom: önek ve android özellikleri için android: öneki kullanın.

Eğer bir stili (res/values/styles.xml) Bu kullanmak istiyorsanız,. son bir şey: ^em>değilxmlns:custom satırı ekleyin. Sadece önek ile öznitelik adı referans:

<style name="MyStyle>
    <item name="typeface">roboto</item>
</style>

                               (PREVIOUS ANSWER)

Using a custom typeface in Android

Bu yardımcı olacaktır. Temelde, XML bunu yapmak için, ve bildiğim kadarıyla daha kolay bir şekilde kod bunu yapmak için bir yol yok. Her zaman bir setLayoutFont olabilir() bir kez yazı karakteri oluşturur, setTypeface çalıştıktan sonra bu yöntem() her biri için. Sadece bir düzen için yeni bir öğe eklemek her zaman güncellemek lazım. Sanki aşağıda:

public void setLayoutFont() {
    Typeface tf = Typeface.createFromAsset(
        getBaseContext().getAssets(), "fonts/BPreplay.otf");
    TextView tv1 = (TextView)findViewById(R.id.tv1);
    tv1.setTypeface(tf);

    TextView tv2 = (TextView)findViewById(R.id.tv2);
    tv2.setTypeface(tf);

    TextView tv3 = (TextView)findViewById(R.id.tv3);
    tv3.setTypeface(tf);
}

EDİT: Ben sadece bu gibi bir işlevi yapıyordu yaparken buldum nasıl: bu benim gibi bir şey uygulamak için, ve var

public static void setLayoutFont(Typeface tf, TextView...params) {
    for (TextView tv : params) {
        tv.setTypeface(tf);
    }
}

OnCreate bu yöntemi kullanın () sonra, güncellemek istediğiniz tüm TextViews pass:

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/BPreplay.otf");
//find views by id...
setLayoutFont(tf, tv1, tv2, tv3, tv4, tv5);

9/5/12 DÜZENLEME:

Bu hala görüş ve oylarını almak olduğu için, çok daha iyi ve daha kapsamlı bir yöntem eklemek istiyorum:

Typeface mFont = Typeface.createFromAsset(getAssets(), "fonts/BPreplay.otf");
ViewGroup root = (ViewGroup)findViewById(R.id.myrootlayout);
setFont(root, mFont);

/*
 * Sets the font on all TextViews in the ViewGroup. Searches
 * recursively for all inner ViewGroups as well. Just add a
 * check for any other views you want to set as well (EditText,
 * etc.)
 */
public void setFont(ViewGroup group, Typeface font) {
    int count = group.getChildCount();
    View v;
    for(int i = 0; i < count; i  ) {
        v = group.getChildAt(i);
        if(v instanceof TextView || v instanceof Button /*etc.*/)
            ((TextView)v).setTypeface(font);
        else if(v instanceof ViewGroup)
            setFont((ViewGroup)v, font);
    }
}

Eğer geçtiğiniz kök düzeniniz olacak yinelemeli olarak kontrol etmek için TextView Button görünümler (veya herhangi bir kişi eklemek için, deyim) içinde düzeni ve yazı olmadan olması belirtin onlar tarafından KİMLİĞİ. Tabii ki bu yazı tipini ayarlamak istediğiniz üstleniyorhergörünüm.

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • StalkerJS

    StalkerJS

    15 HAZİRAN 2010
  • thetrollska

    thetrollska

    2 EKİM 2009
  • Tips On Linux

    Tips On Linu

    26 Temmuz 2008