Typeface xyz=Typeface.createFromAsset(view.getContext().getAssets(),
"fonts/xyz.ttf");
(TextView)abc.setTypeface(xyz);
以下是如何创建和设置自定义字体的方法。
我需要设置 pagertitlestrip
的字体,但是 pagertitlestrip
没有 .setTypeFace
函数。有人知道我怎么做吗?
Typeface xyz=Typeface.createFromAsset(view.getContext().getAssets(),
"fonts/xyz.ttf");
(TextView)abc.setTypeface(xyz);
以下是如何创建和设置自定义字体的方法。
我需要设置 pagertitlestrip
的字体,但是 pagertitlestrip
没有 .setTypeFace
函数。有人知道我怎么做吗?
PagerTitleStrip
实际上是扩展了ViewGroup
,因此您可以只需对其进行子类化并遍历每个子项以查找需要更改字体的视图:
public class CustomFontPagerTitleStrip extends PagerTitleStrip {
public CustomFontPagerTitleStrip(Context context) {
super(context);
}
public CustomFontPagerTitleStrip(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/xyz.ttf");
for (int i=0; i<this.getChildCount(); i++) {
if (this.getChildAt(i) instanceof TextView) {
((TextView)this.getChildAt(i)).setTypeface(tf);
}
}
}
}
public class WeeklyFragment extends Fragment{
PagerTitleStrip _Title;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.weekly_fragment, container, false);
_Title = (PagerTitleStrip)rootView.findViewById(R.id.__Weekly_pager_title_strip);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "your font.ttf");
for (int counter = 0 ; counter<_Title.getChildCount(); counter++) {
if (_Title.getChildAt(counter) instanceof TextView) {
((TextView)_Title.getChildAt(counter)).setTypeface(font);
((TextView)_Title.getChildAt(counter)).setTextSize(25);
}
}
虽然我认为ViewPager
支持android:textAppearance
,但你也不能通过这种方式设置字体。
最好的方法可能是复制PagerTitleStrip
代码(在你的SDK或在线可用),将其重构为自己的包,并进行修改以适应。或者,看看ViewPagerIndicator库是否有一个你喜欢的,可以配置字体的选项。
if (!isInEditMode())
确保它不在Eclipse中运行。 - jburns20