Android 如何在String.xml文件中使用自定义字体
我在String.xml文件中使用了以下代码。现在我的问题是,我在这里使用了字体面作为天线。那么,有没有什么方法可以使用我的自定义字体而不是天线呢?假设我的自定义字体是aa.ttf…那么我在这里如何使用呢。 我的代码是Android 如何在String.xml文件中使用自定义字体,android,Android,我在String.xml文件中使用了以下代码。现在我的问题是,我在这里使用了字体面作为天线。那么,有没有什么方法可以使用我的自定义字体而不是天线呢?假设我的自定义字体是aa.ttf…那么我在这里如何使用呢。 我的代码是 <string name="strt_dialog_desc">To start monitoring press and hold the date on which the latest cycle started.<![CDATA[<font fac
<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>
要开始监控,请按住最新循环开始的日期。]]>单击选项菜单获取帮助!]]>
//将字体放在资产文件夹中
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);
//将字体放在“资源”文件夹中
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);
您可以通过以下属性设置字体
android:typeFace=
您可以通过以下属性设置字体
android:typeFace=
//使用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);
String str=“Bold
然后是普通文本
然后是斜体“+
“
这是一个简单的句子”+
“
这是一个简单的句子”+
“
这是一个简单的句子”;
span strHtml=Html.fromHtml(str);
TextView tv=(TextView)findViewById(R.id.TextView);
tv.setText(strHtml);
//使用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);
String str=“Bold
然后是普通文本
然后是斜体“+
“
这是一个简单的句子”+
“
这是一个简单的句子”+
“
这是一个简单的句子”;
span strHtml=Html.fromHtml(str);
TextView tv=(TextView)findViewById(R.id.TextView);
tv.setText(strHtml);
我认为您也可以使用此选项,这将在运行时更改字体
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>
衬线
我认为您也可以使用此选项,这将在运行时更改字体
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>
衬线
我知道这一点,但我的问题是如何在string.xml文件中实现这一点,这是不可能的。字体是在文本视图上设置的,我知道这一点,但我的问题是如何在string.xml文件中设置,这是不可能的。字体是在文本视图上设置的。你只需检查我的代码。因此,在我的代码中,我想使用我的自定义字体而不是天线。你只需检查我的代码。因此,在我的代码中,我想使用我的自定义字体而不是天线。这对我有效,但它只会更改文本视图
s,而不会更改字符串
s名称这对我有效,但是,它只会更改文本视图
s,而不会更改字符串
s名称