SORU
4 HAZİRAN 2010, Cuma


Özel bir yazı tipi kullanarak Android

Yaratıyorum android uygulamam için özel bir yazı tipi kullanmak istiyorum.
Tek tek Kodunu her nesnenin yazı tipi değiştirebilirsiniz, ama bende bunlardan yüzlerce var.

Yani

  • Bir şekilde XML den bunu yapmak için var mı? [Özel yazı tipi ayarı]
  • Bir şekilde bir yerde kodundan, tüm uygulama ve tüm bileşenleri varsayılan bir yerine özel bir yazı tipi kullanması gerektiğini söylemek için var?

Teşekkürler.
-Codevalley

CEVAP
3 Mart 2011, PERŞEMBE


Evet mümkündür.

Metin görünümü uzanan özel bir görünüm oluşturmak zorunda.

values klasör attrs.xml:

<resources>
    <declare-styleable name="MyTextView">
        <attr name="first_name" format="string"/>
        <attr name="last_name" format="string"/>
        <attr name="ttf_name" format="string"/>
    </declare-styleable>
</resources>

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:lht="http://schemas.android.com/apk/res/com.lht"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView  android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="Hello"/>
    <com.lht.ui.MyTextView  
        android:id="@ id/MyTextView"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="Hello friends"
        lht:ttf_name="ITCBLKAD.TTF"
        />   
</LinearLayout>

MyTextView.java:

package com.lht.ui;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class MyTextView extends TextView {

    Context context;
    String ttfName;

    String TAG = getClass().getName();

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        for (int i = 0; i < attrs.getAttributeCount(); i  ) {
            Log.i(TAG, attrs.getAttributeName(i));
            /*
             * Read value of custom attributes
             */

            this.ttfName = attrs.getAttributeValue(
                    "http://schemas.android.com/apk/res/com.lht", "ttf_name");
            Log.i(TAG, "firstText "   firstText);
            // Log.i(TAG, "lastText "  lastText);

            init();
        }

    }

    private void init() {
        Typeface font = Typeface.createFromAsset(context.getAssets(), ttfName);
        setTypeface(font);
    }

    @Override
    public void setTypeface(Typeface tf) {

        // TODO Auto-generated method stub
        super.setTypeface(tf);
    }

}

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Derek Banas

    Derek Banas

    12 AĞUSTOS 2008
  • LiteralMSPaint

    LiteralMSPai

    27 EKİM 2010
  • Rickymon Tero

    Rickymon Ter

    1 Ocak 2007