Tương đương với “android: fontFamily =” sans-serif-light ”trong mã Java là gì?


106

Câu hỏi của tôi khá đơn giản:

Trong mỗi lần xem văn bản của tôi, tôi hiện đang sử dụng thuộc tính

android:fontFamily="sans-serif-light"

để cung cấp một cái nhìn tuyệt đẹp trên các thiết bị HC.

Thật không may, điều này không hoạt động với mọi tiện ích và đối với Spinners của tôi, tôi cần ghi đè Bộ điều hợp.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //You can use the new tf here.

    if(convertView == null || convertView.getTag() == null) {
        // new view - populate 
        convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
        convertView.setTag(new Object());
    }

    CheckedTextView spinner_text=(CheckedTextView) convertView.findViewById(android.R.id.text1);
    //Typeface should be set here...
    return spinner_text;
    }
}

Vì vậy, có cách nào để nhận được cùng một kết quả bằng mã không?

Tái bút: Không, tôi không muốn đặt một kiểu chữ trong thư mục tài sản, tôi chỉ muốn sử dụng hệ thống một.

Câu trả lời:


168

Nó sẽ có thể với setTypeface()Typeface.create():

convertView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));

Xem Tài liệu :

Tạo một đối tượng kiểu chữ có tên họ và thông tin kiểu tùy chọn. Nếu null được chuyển cho tên, thì phông chữ "mặc định" sẽ được chọn. Đối tượng kiểu chữ kết quả có thể được truy vấn ( getStyle()) để khám phá đặc điểm kiểu "thực" của nó là gì.

Lưu ý rằng sử dụng quá nhiều Typeface.create()sẽ có hại cho trí nhớ của bạn, như đã nêu trong nhận xét này . Các Hashtable gợi ý là một giải pháp tốt, nhưng bạn cần phải sửa đổi nó một chút vì bạn không tạo kiểu chữ của bạn từ một tài sản.


2
Hãy cẩn thận với các vấn đề về bộ nhớ nếu bạn có nhiều đồ quay - Tôi đã cập nhật câu trả lời của mình một chút.
saschoar

44

Android 4.1 (API cấp 16) và Thư viện hỗ trợ 26 trở lên

Nếu bạn đang sử dụng thư mục res -> font, bạn có thể sử dụng như thế này

  val typeface = ResourcesCompat.getFont(Context, R.font.YOUR_FONT)
  TextView.setTypeface(typeface)

20

Theo ý kiến ​​của tôi, vẫn có một cách để áp dụng phông chữ hệ thống theo chương trình trên TextView mà không gặp bất kỳ sự cố bộ nhớ nào và đó là sử dụng textview.setTextAppearancephương pháp:

<style name="styleA">
    <item name="android:fontFamily">sans-serif</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">?android:attr/textColorPrimary</item>
</style>
<style name="styleB">
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">?android:attr/textColorTertiary</item>
</style>


if(condition){
    textView.setTextAppearance(context,R.style.styleA);
}else{
    textView.setTextAppearance(context,R.style.styleB);
}

2
android:fontFamilyyêu cầu API 16.
CoolMind

2
Sử dụng TextViewCompat.setTextAppearance(textView, R.style.styleA).
CoolMind

19

Tự động bạn có thể đặt fontfamily tương tự như android: fontFamily trong xml bằng cách sử dụng điều này,

For Custom font:

 TextView tv = ((TextView) v.findViewById(R.id.select_item_title));
 Typeface face=Typeface.createFromAsset(getAssets(),"fonts/mycustomfont.ttf"); 
 tv.setTypeface(face);

For Default font:

 tv.setTypeface(Typeface.create("sans-serif-medium",Typeface.NORMAL));

Đây là danh sách họ phông chữ mặc định được sử dụng, hãy sử dụng bất kỳ họ phông chữ nào trong số này bằng cách thay thế chuỗi dấu ngoặc kép "sans-serif-medium"

FONT FAMILY                    TTF FILE                    

1  casual                      ComingSoon.ttf              
2  cursive                     DancingScript-Regular.ttf   
3  monospace                   DroidSansMono.ttf           
4  sans-serif                  Roboto-Regular.ttf          
5  sans-serif-black            Roboto-Black.ttf            
6  sans-serif-condensed        RobotoCondensed-Regular.ttf 
7  sans-serif-condensed-light  RobotoCondensed-Light.ttf   
8  sans-serif-light            Roboto-Light.ttf            
9  sans-serif-medium           Roboto-Medium.ttf           
10  sans-serif-smallcaps       CarroisGothicSC-Regular.ttf 
11  sans-serif-thin            Roboto-Thin.ttf             
12  serif                      NotoSerif-Regular.ttf       
13  serif-monospace            CutiveMono.ttf              

"mycustomfont.ttf" là tệp ttf. Đường dẫn sẽ có trong src / asset / fonts / mycustomfont.ttf , bạn có thể tham khảo thêm về font mặc định trong họ Default font này


13

Tùy chọn 1 - API 26 trở lên

// Jave
Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);

// Kotlin
val typeface = resources.getFont(R.font.myfont)
textView.typeface = typeface

Tùy chọn 2 - API 16 trở lên

// Java
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);

// Kotlin
val typeface = ResourcesCompat.getFont(context, R.font.myfont)

Kiểm tra thời hạn đầy đủ tại Hướng dẫn dành cho nhà phát triển Android .


6

Nó sẽ có thể bằng cách sử dụng

setTypeface(Typeface tf, int style)phương pháp của TextViewlớp.

spinner_text.setTypeface(Typeface.SANS_SERIF,Typeface.NORMAL);

Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.