2011-11-16 97 views
1

我在String.xml文件中使用下面的代碼。現在我的問題是在這裏,我使用了字體作爲Aerial.So有什麼方法可以使用我的自定義字體在這裏使用天線。假設我的自定義字體是aa.ttf ...所以我怎麼能在這裏使用。 我的代碼是如何在String.xml文件中使用自定義字體

<string name="strt_dialog_desc">To start monitoring press and hold the date on which the latest cycle started.<![CDATA[<font face=Aerial>]]>Click OptionMenu for help!<![CDATA[</font>]]></string> 

回答

3

//將您的字體資產文件夾

TextView tv=(TextView)findViewById(R.id.custom); 
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/aerial.ttf"); 

tv.setTypeface(face); 
tv.setText(R.string.strt_dialog_desc); 
+0

我知道的事情,但我的問題是如何做到這一點的string.xml文件 – AndroidDev

+0

這是不可能的。在textviews上設置字體 – Kuffs

-1

您可以通過下面的屬性設置字體

android:typeFace= 
+0

如何在我的上面的代碼中使用這個 – AndroidDev

+0

您只需檢查我的代碼。因此,在我的代碼中,我想將自定義字體替換爲空中的 – AndroidDev

2

//使用動態SpannableString更改字體

Typeface tfaerial=Typeface.createFromAsset(getAssets(),"fonts/aerial.ttf"); 
Typeface tfTradeGothicLight=Typeface.createFromAsset(getAssets(), "fonts/TradeGothic-Light.OTF"); 

String strt_dialog_desc=this.getResources().getString(R.string.strt_dialog_desc); 

int upto=strt_dialog_desc.indexOf("."); 

     if (strt_dialog_desc!=null) 
     { 
      aboutAuthTV.setTextColor(Color.BLACK); 
      aboutAuthTV.setLineSpacing(1.2f, 1.5f); 
      aboutAuthTV.setTextSize(23); 

      SpannableString SS = new SpannableString(strt_dialog_desc); 
      SS. setSpan (new StyleSpan(tfTradeGothicLight.getStyle()), 0, upto,Spanned.SPAN_EXCLUSIVE_INCLUSIVE); 
      SS. setSpan (new StyleSpan(tfaerial.getStyle()), upto, strt_dialog_desc.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE); 
      yourtextView.setText(SS); 
     } 

//這個以這種方式改變字體改變顏色notworking

String str="<font size =\"20\"><B>Bold</B> <br/> Then Normal Text<br/> 
         <i>Then Italic</i> </font>" + 
         "<br/> <font color=\"green\" >this is simple sentence </font>" + 
         "<br/> <font face=\"verdana\" >this is simple sentence </font>"+ 
         "<br/><br/><br/><br/><a>this is simple sentence</a>"; 
     Spanned strHtml= Html.fromHtml(str); 

     TextView tv = (TextView)findViewById(R.id.textView); 
     tv.setText(strHtml); 
2

我認爲你可以使用這個code這將在運行時更改字體這樣做。

MyApp.java

public class MyApp extends Application { 

    @Override 
    public void onCreate() { 
    TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); // font from assets: "assets/fonts/Roboto-Regular.ttf 
    } 
} 

TypefaceUtil.java

import android.content.Context; 
import android.graphics.Typeface; 
import android.util.Log; 

import java.lang.reflect.Field; 

public class TypefaceUtil { 

    /** 
    * Using reflection to override default typeface 
    * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN 
    * @param context to work with assets 
    * @param defaultFontNameToOverride for example "monospace" 
    * @param customFontFileNameInAssets file name of the font from assets 
    */ 
    public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) { 
     try { 
      final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets); 

      final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride); 
      defaultFontTypefaceField.setAccessible(true); 
      defaultFontTypefaceField.set(null, customFontTypeface); 
     } catch (Exception e) { 
      Log.e("Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride); 
     } 
    } 
} 

的themes.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light"> 
    <!-- you should set typeface which you want to override with TypefaceUtil --> 
    <item name="android:typeface">serif</item> 
    </style> 
</resources> 
+0

這對我有效,但它只會改變「TextView不是'字符串的名字 –

相關問題