Thứ Năm, 27 tháng 11, 2014

Android Using External Fonts

http://javatechig.com/android/using-external-fonts-in-android-view

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

public class CustomTextView extends TextView {
 public CustomTextView(Context context) {
  super(context);
  setFont();
 }
 public CustomTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
  setFont();
 }
 public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  setFont();
 }

 private void setFont() {
  Typeface font = Typeface.createFromAsset(getContext().getAssets(),
    "fonts/Maximum.ttf");
  setTypeface(font, Typeface.NORMAL);
 }
}
Now change the following in your android layout xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#0F0F0F">

<com.javatechig.droid.ui.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/font_sample_text"
android:textSize="18dp"
android:textColor="#AAD4DE" />

<com.javatechig.droid.ui.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/font_sample_text"
android:textColor="#FFDE46"
android:textSize="24dp"/>

<com.javatechig.droid.ui.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/font_sample_text"
android:textSize="30dp"
android:textColor="#7FBA00"/>

</LinearLayout>

Không có nhận xét nào:

Đăng nhận xét