2010-04-01 24 views
26

Tôi đang định nghĩa XML kiểu cho ứng dụng Android của mình. Tôi có một số tập tin TTF tôi muốn sử dụng, làm thế nào tôi có thể thiết lập kiểu chữ để sử dụng những tập tin như phông chữ trái với "sans" chung, "serif" & "monospace". Cảm ơnĐặt phông chữ cụ thể trong một styles.xml

Trả lời

47

Bạn chỉ có thể sử dụng phông chữ tùy chỉnh thông qua mã Java, không phải thông qua XML bố cục hoặc kiểu/chủ đề - xin lỗi!

+0

Thanks a lot Tôi đánh giá cao nó. – NickTFried

+0

@ SergiCastellsaguéMillán: Câu hỏi này dành cho việc sử dụng phông chữ TTF. Câu trả lời bạn liên kết là dành cho mọi thứ ** nhưng ** phông chữ TTF. – CommonsWare

+1

Vì đây là câu trả lời cũ; bạn có biết liệu điều này có thay đổi gì trong các phiên bản sau không, @CommonsWare? Tôi cũng rất thích có thể thay đổi phông chữ bằng cách xác định phông chữ trong tài liệu ''. –

7
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium"> 
     <item name="android:layout_width">fill_parent</item> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:textColor">#00FF00</item> 
     <item name="android:typeface">monospace</item> 
    </style> 
</resources> 
+2

monspace ở đây là gì ??? nếu tôi có tệp .ttf – sheetal

+1

https://en.wikipedia.org/wiki/Monospaced_font là loại phông chữ –

+1

nếu tôi muốn sử dụng sans-serif thì sao? – Senhor

16

TextViewPlus.java:

package com.example; 

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

public class TextViewPlus extends TextView { 
    private static final String TAG = "TextView"; 

    public TextViewPlus(Context context) { 
     super(context); 
    } 

    public TextViewPlus(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setCustomFont(context, attrs); 
    } 

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setCustomFont(context, attrs); 
    } 

    private void setCustomFont(Context ctx, AttributeSet attrs) { 
     TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus); 
     String customFont = a.getString(R.styleable.TextViewPlus_customFont); 
     setCustomFont(ctx, customFont); 
     a.recycle(); 
    } 

    public boolean setCustomFont(Context ctx, String asset) { 
     Typeface tf = null; 
     try { 
     tf = Typeface.createFromAsset(ctx.getAssets(), asset); 
     } catch (Exception e) { 
      Log.e(TAG, "Could not get typeface: "+e.getMessage()); 
      return false; 
     } 

     setTypeface(tf); 
     return true; 
    } 

} 

attrs.xml: (trong res/values)

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="TextViewPlus"> 
     <attr name="customFont" format="string"/> 
    </declare-styleable> 
</resources> 

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:foo="http://schemas.android.com/apk/res/com.example" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <com.example.TextViewPlus 
     android:id="@+id/textViewPlus1" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:text="@string/showingOffTheNewTypeface" 
     foo:customFont="saxmono.ttf"> 
    </com.example.TextViewPlus> 
</LinearLayout> 

Bạn sẽ đặt "saxmono.ttf" trong tài sản thư mục.

0

Có, bạn có thể :)

Bước 1: Tạo thư mục và đặt tên là 'font' và .ttf bên trong thư mục. enter image description here

bước 2: Đến style.xml và làm như sau: - enter image description here

bước 3: Sử dụng các tag style trong bất kỳ đối tượng UI: -

enter image description here

+0

Trước khi tất cả điều này, trước tiên, bạn cần phải cập nhật để cập nhật để hỗ trợ thư viện 26 đây là một liên kết https://segunfamisa.com/posts/custom-fonts-with-android-support-library –

Các vấn đề liên quan