TextViewカスタムフォントとハイライトの使用

2741 ワード

オリジナルを尊重:http://blog.csdn.net/yuanzeyao/article/details/40478815
今では多くのアプリケーションで自分で定義したフォントを使うのが好きですが、今日はTextViewで自分で定義したフォントを使っています.ハイライトを設定し、次のような効果を表示します.
TextView 使用自定义的字体和亮点_第1张图片
LedTextViewを定義します.java
/**
 *      TextView
 * com.led.demo.LedTextView
 * @author yuanzeyao <br/>
 * create at 2014 10 26    6:46:05
 */
public class LedTextView extends TextView
{
  private static final String TAG = "LedTextView";
  
  public LedTextView(Context context)
  {
    super(context);
    init(context);
  }
  
  public LedTextView(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    init(context);
  }

  public LedTextView(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);
    init(context);
  }
  
  private void init(Context context)
  {
    //      assets   
    AssetManager am=context.getAssets();
    Typeface font=Typeface.createFromAsset(am, "fonts/digital-7.ttf");
    setTypeface(font);
  }

レイアウトファイルでLedTextViewを参照
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:background="@android:color/black"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.led.demo.LedTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#00ff00"
        android:text="12:00:00"
        android:shadowColor="#00ff00"
        android:shadowDx="0"
        android:shadowDy="0"
        android:textSize="20sp" 
        />

</RelativeLayout>

インストールが完了しました.上記の結果を表示できます
本文のブログのオリジナルの文章、ブログ、同意を得ないで、転載してはいけません.