2015-11-14 22 views
2

我想設置不同的字體樣式我的'按鈕'文本和'編輯文本框'文本像時代的新羅馬,Calibri,坎布里亞和格魯吉亞。我如何設置不同的字體,例如我想將我的登錄按鈕文本更改爲Calibri字體。我不知道如何設置或從MS Office或字體文件導入字體。請給我建議,謝謝。如何在Android中設置自定義不同的字體樣式

我的XML代碼在這裏

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint=" User Name " /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint=" Password " 
     android:fontFamily="Tekton Pro Ext"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fontFamily="Tekton Pro Ext" 
     android:text=" Login " /> 
</LinearLayout> 

佈局

enter image description here

回答

3

您需要創建字體文件夾資產文件夾下在你的項目中,並把你的TTF和它在主或在你的活動,你可以設置字體作爲

TextView myTextView=(TextView)findViewById(R.id.textBox); 
Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf"); 
myTextView.setTypeface(typeFace); 
1

也許它能夠更好地看看這個帖子究竟哪個回答你正在尋找:

1- How to change fontFamily of TextView in Android

2- How to change the font on the TextView?

短你必須做這樣的事情: 把字體在/ fonts目錄Assets文件夾中,然後使用該行代碼:

Button bt = (Button) findViewById(R.id.myButton); 
Typeface face = Typeface.createFromAsset(getAssets(), 
      "fonts/epimodem.ttf"); 
bt.setTypeface(face); 
0

以上所有答案都是正確的。如果您想在整個應用中使用不同字體的ButtonTextView,請執行以下操作。

public class FontsArePriceyTextView extends TextView { 

    public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setCostlyFont(); 
    } 

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setCostlyFont(); 
    } 

    public MyTextView(Context context) { 
     super(context); 
     setCostlyFont(); 
    } 

    private void setCostlyFont() { 
     if (!isInEditMode()) { 
      Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "filenameofyourfont.ttf"); 
      setTypeface(tf); 
     } 
    } 
} 

然後在您的佈局中,將TextView替換爲yourpackagename.FontsArePriceyTextView。例如。

<com.company.projectname.widgets.FontsArePriceyTextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Your awesome stylised text" /> 

你應該很好走。

+0

如何使用這個構造函數類的所有文本字段可以訪問我的項目的所有文本視圖? – wingsraam

+0

只需使用您的自定義文本視圖類名稱替換XML佈局中的TextView標記即可。您可以將其用作普通文本視圖,例如。 'TextView tv =(TextView)findViewById(R.id.stylisedTextView)''。我想你現在可以實現按鈕或任何你想要的自己。 –

+0

任何可能爲新字體添加直接的XML文本視圖 – wingsraam

相關問題