我正在创建一个用于重新加载的android按钮。它想要在按钮中同时包含图标和文本,所以我使用的是fontawesome。但是如何在按钮文本上同时应用我在我的应用程序中使用的fontawesome字体和自定义字体。
答案 0 :(得分:7)
这就是我实现它的方法,并使用了https://stackoverflow.com/a/10741161/992665
中的CustomTypefaceSpan类public class MainActivity extends Activity {
Typeface tf_r, tf_icon;
Button reload;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int hexVal = Integer.parseInt("f021", 16);
SpannableStringBuilder SS = new SpannableStringBuilder((char)hexVal + " Refresh");
tf_r = Typeface.createFromAsset(this.getAssets(), "www/fonts/Roboto-Light.ttf");
tf_icon = Typeface.createFromAsset(this.getAssets(), "www/fonts/fontawesome-webfont.ttf");
SS.setSpan(new CustomTypefaceSpan("", tf_icon), 0, 1,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
SS.setSpan(new CustomTypefaceSpan("", tf_r), 1, 9,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
reload = (Button) findViewById(R.id.no_openings_reload);
reload.setText(SS);
}
}
答案 1 :(得分:2)
尝试以下代码: -
String text = "This is an example";
Spannable s = new SpannableString(text + " text");
s.setSpan(new TypefaceSpan("monospace"), 0, text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new TypefaceSpan("serif"), text.length(), s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
((TextView)findViewById(R.id.my_text_view)).setText(s);
或
TextView myTextView=(TextView)findViewById(R.id.textBox);
Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
myTextView.setTypeface(typeFace);
或
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/verdana.ttf");
textfield.setTypeface(tf,Typeface.BOLD);
请参阅以下链接了解更多信息: -
http://www.androidhive.info/2012/02/android-using-external-fonts/
http://www.tutorialspoint.com/android/android_custom_fonts.htm