"android: fontFamily =" sans-serif-light "ในโค้ด Java เทียบเท่ากับอะไร


106

คำถามของฉันค่อนข้างง่าย:

ในทุกมุมมองข้อความของฉันฉันกำลังใช้แอตทริบิวต์

android:fontFamily="sans-serif-light"

เพื่อให้รูปลักษณ์ที่สวยงามบนอุปกรณ์โพสต์ HC

น่าเสียดายที่สิ่งนี้ใช้ไม่ได้กับทุกวิดเจ็ตและสำหรับ Spinners ของฉันฉันจำเป็นต้องเขียนทับ Adapter

@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;
    }
}

ดังนั้นมีวิธีที่จะได้ผลลัพธ์เดียวกันทุกประการโดยใช้รหัสหรือไม่?

PS: ไม่ฉันไม่ต้องการใส่แบบอักษรในโฟลเดอร์เนื้อหาฉันแค่ต้องการใช้ระบบเดียว

คำตอบ:


170

ควรเป็นไปได้ด้วยsetTypeface()และTypeface.create():

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

ดูเอกสาร :

สร้างออบเจ็กต์แบบอักษรที่มีชื่อสกุลและข้อมูลสไตล์ตัวเลือก หากมีการส่งค่าว่างสำหรับชื่อระบบจะเลือกแบบอักษร "เริ่มต้น" อ็อบเจ็กต์แบบอักษรที่เป็นผลลัพธ์สามารถgetStyle()สืบค้น( ) เพื่อดูว่าลักษณะสไตล์ "ของจริง" เป็นอย่างไร

โปรดทราบว่าการใช้งานมากเกินไปTypeface.create()นั้นไม่ดีต่อความจำของคุณดังที่ระบุไว้ในความคิดเห็นนี้ ปัญหา Hashtableเป็นทางออกที่ดี แต่คุณต้องปรับเปลี่ยนเล็ก ๆ น้อย ๆ ตั้งแต่คุณไม่ได้สร้างตัวอักษรของคุณจากสินทรัพย์


2
โปรดระวังปัญหาเกี่ยวกับหน่วยความจำหากคุณมีรายการสปินเนอร์จำนวนมาก - ฉันอัปเดตคำตอบของฉันเล็กน้อย
saschoar

44

Android 4.1 (API ระดับ 16) และ Support Library 26 ขึ้นไป

หากคุณใช้โฟลเดอร์ res -> font คุณสามารถใช้แบบนี้ได้

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

21

ในความคิดของฉันยังมีวิธีการใช้แบบอักษรของระบบโดยทางโปรแกรมบน TextView โดยไม่มีปัญหาหน่วยความจำใด ๆและใช้textview.setTextAppearanceวิธีการ:

<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:fontFamilyต้องการ API 16
CoolMind

2
ใช้TextViewCompat.setTextAppearance(textView, R.style.styleA).
CoolMind

20

แบบไดนามิกคุณสามารถตั้งค่า fontfamily ที่คล้ายกับ android: fontFamily ใน xml โดยใช้สิ่งนี้

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));

นี่คือรายการตระกูลฟอนต์เริ่มต้นที่ใช้ใช้สิ่งเหล่านี้โดยแทนที่สตริงอัญประกาศคู่"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" คือไฟล์ ttf เส้นทางจะอยู่ในsrc / assets / ฟอนต์ / mycustomfont.ttfคุณสามารถอ้างอิงเพิ่มเติมเกี่ยวกับฟอนต์เริ่มต้นในตระกูลฟอนต์เริ่มต้นนี้


13

ตัวเลือก 1 - API 26 และสูงกว่า

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

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

ตัวเลือก 2 - API 16 และสูงกว่า

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

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

ตรวจสอบยัญเต็มได้ที่Android คู่มือการพัฒนา


6

มันจะเป็นไปได้โดยใช้

setTypeface(Typeface tf, int style)วิธีการTextViewเรียน

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

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.