2013-04-24 113 views
9

我想在Arial字體中顯示文本。但Arial字體在android系統字體中不可用。我不想在我的應用程序中使用arial ttf文件。有沒有其他的方式來使用Arial字體應用文本。Arial字體的Android中的文本

+3

不可以。任何固有的字體都需要'.ttf' /'.otf'。 – 2013-04-24 05:02:18

+0

我可以直接在我的應用程序中使用ttf文件嗎?有沒有許可問題? – Keerthi 2013-04-24 05:24:05

+5

並非所有字體都可以免費用於商業目的。所以一定要在使用之前進行檢查。話雖如此,我通常一直到http://www.fontsquirrel.com/,它擁有大量的免費字體。比對不起更安全。 ;-)至於Arial字體的許可,請查看Wikipedia上的此鏈接:http://en.wikipedia.org/wiki/Arial#Free_alternatives – 2013-04-24 05:27:30

回答

9

如果字體在android系統中不可用,那麼您必須使用字體文件來將該字體應用於您的textView。我可以知道爲什麼你不願意使用字體文件來應用它,因爲它提供了相同的功能。

使用字體文件使用範例是:

Typeface tfArial = Typeface.createFromAsset(getAssets(), "arial.ttf"); 
TextView tv = null; 
// find the textview id from layout or create dynamically 
tv.setTypeface(tfArial); 

編輯:

你需要把字體文件arial.ttfasset文件夾中。

+0

您必須在您的應用程序中使用Arial字體。 – AndroidEnthusiastic 2013-04-24 06:33:41

4

下載Arial字體和資產創建的文件夾名稱以「字體」,並在那裏

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/arial.ttf"); 
TextView tv = (TextView) findViewById(R.id.CustomFontText); 
tv.setTypeface(tf); 
0

保存字體考慮用書法來在你的Android應用程序的任何自定義字體。

您不需要在每個TextView中添加代碼來應用字體。只需在應用程序啓動時初始化,您就可以開始。

0

創建Java代碼中的TextView或XML這樣

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@android:id/text1" 
android:layout_width="match_parent" 
android:textSize="15sp" 
android:textColor="@color/tabs_default_color" 
android:gravity="center" 
android:layout_height="match_parent" 
/> 

請務必保持的ID,因爲它是在這裏,因爲這個ID的TabLayout檢查,如果您使用自定義的TextView

然後從代碼充氣此佈局並在該文本視圖上設置自定義Typeface,並將此自定義視圖添加到該選項卡

for (int i = 0; i < tabLayout.getTabCount(); i++) { 
    //noinspection ConstantConditions 
TextView tv=(TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null) 
tv.setTypeface(Typeface);  
tabLayout.getTabAt(i).setCustomView(tv); 

} 
相關問題