Özel bir yazı tipi kullanarak Android | Netgez.com
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

  • DudeFromUkraine

    DudeFromUkra

    7 Ocak 2008
  • Electro Posé

    Electro PosÃ

    21 ÅžUBAT 2013
  • Photoshop Tutorials

    Photoshop Tu

    22 HAZİRAN 2011