2012-02-28 35 views
23

Tôi đang cố tạo chế độ xem văn bản tùy chỉnh có phông chữ được đặt từ một đường dẫn nhất định. Vui lòng cung cấp cho tôi bất kỳ ví dụ và làm thế nào tôi có thể làm điều đó với mã ít:Cách tạo TextView tùy chỉnh?

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/accountInfoText" 
    android:textColor="#727272" 
    android:textSize="18dp" /> 
+1

Tham khảo [này] (http://developer.android.com/guide/topics/ui/themes.html) – Ghost

Trả lời

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

public class FontTextView extends TextView { 


   public FontTextView(Context context) { 
     super(context); 
     Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
     this.setTypeface(face); 
   } 

   public FontTextView(Context context, AttributeSet attrs) { 
       super(context, attrs); 
    Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
 this.setTypeface(face); 
   } 

   public FontTextView(Context context, AttributeSet attrs, int defStyle) { 
       super(context, attrs, defStyle); 
    Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
 this.setTypeface(face); 
   } 

   protected void onDraw (Canvas canvas) { 
       super.onDraw(canvas); 
        
      
   } 

} 

và trong xml:

<com.util.FontTextView 
                   android:id="@+id/textView2" 
                   android:layout_width="wrap_content" 
                   android:layout_height="wrap_content" 
                   android:text="@string/accountInfoText" 
                   android:textColor="#727272" 
                   android:textSize="18dp" /> 
+28

tôi khuyên bạn nên sử dụng một singleton để áp dụng các phông chữ để tránh tạo kiểu chữ từ nội dung mỗi lần. – JackMahoney

+0

@JackMahoney +1 cho nhận xét rất hữu ích của bạn. Bây giờ ứng dụng của tôi rất nhanh !! –

+0

@JackMahoney bạn có ví dụ về chuyển đổi chế độ xem tùy chỉnh thành singleton để hiển thị xung quanh không? Sẽ hữu ích. – kabuto178

-1

dưới đây lớp tùy chỉnh có thể giúp bạn tùy biến cho việc thiết lập font chữ yêu cầu của bạn trên TextView vì vậy bạn phải đặt một số tệp .ttf vào nội dung và cung cấp đường dẫn này theo tùy chỉnh TextView.

public class TextViewBoldFont extends TextView { 
    public TextViewBoldFont(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     String fontPath = "GOTHICB.TTF"; 
     Typeface fontsStyle = Typeface.createFromAsset(context.getAssets(), fontPath); 
     this.setTypeface(fontsStyle,Typeface.BOLD); 
    } 

    public TextViewBoldFont(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     String fontPath = "GOTHICB.TTF"; 
     Typeface fontsStyle = Typeface.createFromAsset(context.getAssets(), fontPath); 
     this.setTypeface(fontsStyle,Typeface.BOLD); 
    } 

    public TextViewBoldFont(Context context) { 
     super(context); 

     String fontPath = "GOTHICB.TTF"; 
     Typeface fontsStyle = Typeface.createFromAsset(context.getAssets(), fontPath); 
     this.setTypeface(fontsStyle,Typeface.BOLD); 
    } 
} 
+2

Xin chào, chào mừng bạn đến với SO. Xin vui lòng không đổ mã như một câu trả lời, giải thích đào tạo của bạn suy nghĩ để người dùng có thể hiểu những gì bạn đang làm. Cảm ơn. – Cthulhu

18

Tạo chế độ xem tùy chỉnh cho Textview. Bước 1 - Thực hiện mục nhập trong tệp attrs.xml và cung cấp tùy chọn để chọn Phông chữ làm danh sách trong tùy chỉnh TextView.

<declare-styleable name="CustomFontTextView"> 
    <attr name="fontName"/> 
</declare-styleable> 

Bước 2: Tạo mục enum với danh sách các phông chữ và gán giá trị duy nhất

<attr name="fontName" format="enum"> 
    <enum name="Roboto_Bold" value="1" /> 
    <enum name="Roboto_Italic" value="2" /> 
    <enum name="Roboto_Light" value="3" /> 
    <enum name="Roboto_Medium" value="4" /> 
    <enum name="Roboto_Regular" value="5" /> 
    <enum name="Roboto_Thin" value="6" /> 
</attr> 

Bước 3: Tạo mục của tất cả các phông chữ trong strings.xml

<string name="Roboto_Bold">Roboto-Bold</string> 
<string name="Roboto_Medium">Roboto-Medium</string> 
<string name="Roboto_Light">Roboto-Light</string> 
<string name="Roboto_Regular">Roboto-Regular</string> 
<string name="Roboto_Thin">Roboto-Thin</string> 
<string name="Roboto_Italic">Roboto-Italic</string> 

Bước 4: tạo thư mục tài sản và sao chép tất cả các phông chữ cần thiết mà bạn muốn đặt trong thư mục phông chữ

Bước 5: Tạo một cla ss mở rộng TextView

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

/** 
* Created by ANKIT 
*/ 
public class CustomFontTextView extends TextView { 

    String customFont; 

    public CustomFontTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     style(context, attrs); 
    } 

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     style(context, attrs); 

    } 

    private void style(Context context, AttributeSet attrs) { 

     TypedArray a = context.obtainStyledAttributes(attrs, 
       R.styleable.CustomFontTextView); 
     int cf = a.getInteger(R.styleable.CustomFontTextView_fontName, 0); 
     int fontName = 0; 
     switch (cf) 
     { 
      case 1: 
       fontName = R.string.Roboto_Bold; 
       break; 
      case 2: 
       fontName = R.string.Roboto_Italic; 
       break; 
      case 3: 
       fontName = R.string.Roboto_Light; 
       break; 
      case 4: 
       fontName = R.string.Roboto_Medium; 
       break; 
      case 5: 
       fontName = R.string.Roboto_Regular; 
       break; 
      case 6: 
       fontName = R.string.Roboto_Thin; 
       break; 
      default: 
       fontName = R.string.Roboto_Regular; 
       break; 
     } 

     customFont = getResources().getString(fontName); 

     Typeface tf = Typeface.createFromAsset(context.getAssets(), 
       "font/" + customFont + ".ttf"); 
     setTypeface(tf); 
     a.recycle(); 
    } 
} 

Bạn có thể sử dụng lớp tùy chỉnh này theo cách này. .. sử dụng packageName.ClassName bạn

<ankit.com.customui.CustomFontTextView 
    android:layout_width="match_parent" 
    android:text="Hello World Ankit" 
    android:textSize="16sp" 
    app:fontName="Roboto_Medium" 
    android:layout_height="wrap_content"/> 
+3

thực hiện tốt hơn nhiều so với câu trả lời được chấp nhận. –