字体真棒在Viewpager previous下一页 [英] Font Awesome in Viewpager Previous Next
问题描述
我试图设置字体真棒箭头为previous'和'下一个',而不是我的网页标题。我如何做到这一点?我知道如何在手动的话加,现在我怎样才能改变这种状况在我strings.xml中定义?引用的箭头
字样字体= Typeface.createFromAsset(getAssets(),字体/ fontawesome-webfont.ttf);
@覆盖
公共字符串getPageTitle(INT位置){ 如果(位置== CURRENT_POSITION - 1){
字符串title =previous
返回称号;
}否则如果(位置== CURRENT_POSITION + 1){
字符串title =下一步;
返回称号;
}
返回称号;
}
对于未来的读者来说,这是根据所提供的答案是什么工作:
最终字样字体= Typeface.createFromAsset(getAssets(),字体/ fontawesome-webfont.ttf);
@覆盖
公共CharSequence的getPageTitle(INT位置){
字符串title =;
如果(位置== CURRENT_POSITION - 1){
标题=\\ uf137
SpannableStringBuilder风格=新SpannableStringBuilder(职称);
styled.setSpan(新CustomTypefaceSpan(,字体),0,title.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
// previous
返回风格; }否则如果(位置== CURRENT_POSITION + 1){
//下一个
标题=\\ uf138
SpannableStringBuilder风格=新SpannableStringBuilder(职称);
styled.setSpan(新CustomTypefaceSpan(,字体),0,title.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
返回风格;
}
为了直接申请任何自定义样式到一个文本序列(而不是设置它的视图),可以在<$ C $包裹串C>跨度。不幸的是,框架不具有接受字样
对象( TypefaceSpan
接受字体系列内置跨度对象代替)。
然而,有许多的例子(如<一个href=\"http://stackoverflow.com/questions/4819049/how-can-i-use-typefacespan-or-stylespan-with-a-custom-typeface\">this 之一)创建一个简单的子类,将适用于您的自定义类型的脸。
应用它看起来是这样的:
@覆盖
公共CharSequence的getPageTitle(INT位置){
字体的字体= Typeface.createFromAsset(getAssets(),fontawesome-webfont.ttf);
字符串title =; 如果(位置== CURRENT_POSITION - 1){
标题=\\ uf137
}否则如果(位置== CURRENT_POSITION + 1){
标题=\\ uf138
} SpannableString风格=新SpannableString(职称);
styled.setSpan(新CustomTypefaceSpan(字体),0,title.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 返回风格;
}
I'm trying to set Font Awesome Arrows as the 'previous' and 'next' instead of my page titles. How do I accomplish this? I know how to manually add in the words, now How can I change that to the referenced arrows defined in my strings.xml?
Typeface font = Typeface.createFromAsset( getAssets(), "fonts/fontawesome-webfont.ttf" );
@Override
public String getPageTitle(int position) {
if (position == current_position - 1) {
String title = "Previous";
return title;
} else if (position == current_position + 1) {
String title = "Next";
return title;
}
return title;
}
For future readers, this is what worked based on the answer provided:
final Typeface font = Typeface.createFromAsset(getAssets(), "fonts/fontawesome-webfont.ttf" );
@Override
public CharSequence getPageTitle(int position) {
String title = "";
if (position == current_position - 1) {
title = "\uf137";
SpannableStringBuilder styled = new SpannableStringBuilder(title);
styled.setSpan(new CustomTypefaceSpan("", font), 0, title.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//Previous
return styled;
} else if (position == current_position + 1) {
//Next
title = "\uf138";
SpannableStringBuilder styled = new SpannableStringBuilder(title);
styled.setSpan(new CustomTypefaceSpan("",font), 0, title.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
return styled;
}
In order to apply any custom styling directly to a text sequence (rather than setting it on the view), you can wrap the string in a Span
. Unfortunately, the framework does not have a built-in span object that accepts a Typeface
object (TypefaceSpan
accepts a font family instead).
However, there are many examples (such as this one) for creating a simple subclass that will apply your custom type face.
Applying it would look something like this:
@Override
public CharSequence getPageTitle(int position) {
Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
String title = "";
if (position == current_position - 1) {
title = "\uf137";
} else if (position == current_position + 1) {
title = "\uf138";
}
SpannableString styled = new SpannableString(title);
styled.setSpan(new CustomTypefaceSpan(font), 0, title.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
return styled;
}
这篇关于字体真棒在Viewpager previous下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!