如何在String.xml文件中使用自定义字体

时间:2011-11-16 11:58:45

标签: android

我在String.xml文件中使用以下代码。现在我的问题是在这里我使用字体面作为Aerial.So有什么方法我可以使用我的自定义字体而不是在这里使用天线..假设我的自定义字体是aa.ttf ...所以我可以在这里使用它。 我的代码是

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

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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>

答案 2 :(得分:1)

//动态使用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);
        }

//这用于改变颜色以这种方式改变字体是不工作的

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

答案 3 :(得分:-1)

您可以通过以下属性设置字体

android:typeFace=