Android 在viewpagerindicator中使用自定义字体
在我的项目中,我使用的字体是android:fontFamily=“sans serif light”,并且工作正常 我还使用了library viewpagerindicator。我想在viewpagerindicator中也使用android:fontFamily=“sans serif light”字体,但找不到如何操作Android 在viewpagerindicator中使用自定义字体,android,viewpagerindicator,custom-font,Android,Viewpagerindicator,Custom Font,在我的项目中,我使用的字体是android:fontFamily=“sans serif light”,并且工作正常 我还使用了library viewpagerindicator。我想在viewpagerindicator中也使用android:fontFamily=“sans serif light”字体,但找不到如何操作 我试过在中使用android:fontfalime=“sans serif light”,如果我没有弄错,您想在视图寻呼机指示器中更改标题字体 为了达到这个目的,我改变了
我试过在
中使用android:fontfalime=“sans serif light”
,如果我没有弄错,您想在视图寻呼机指示器中更改标题字体
为了达到这个目的,我改变了图书馆,
对于TabPageIndicator自定义字体,我为TabPageIndicator.java添加了这个
private Typeface mTypeface;
public void setTypeFace(Typeface tf) {
this.mTypeface = tf;
}
然后将addTab函数更改为:
private void addTab(int index, CharSequence text, int iconResId) {
final TabView tabView = new TabView(getContext());
tabView.mIndex = index;
tabView.setFocusable(true);
tabView.setOnClickListener(mTabClickListener);
tabView.setText(text);
**if (mTypeface != null) {
tabView.setTypeface(mTypeface);
}**
if (iconResId != 0) {
tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
}
mTabLayout.addView(tabView, new LinearLayout.LayoutParams(0, MATCH_PARENT, 1));
}
现在,您只需在tabpagerindicator上设置字体,如下所示:
mTabPageIndicator = (TabPageIndicator) findViewById(R.id.tab_page_indicator);
mTabPageIndicator.setTypeFace(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/custome_font.ttf");
我正在使用这个库,下面是我如何使用它的(index是视图页面指示器中的选项卡索引):
下面是我如何得到这份表格的:
tabLayout = (ViewGroup)indicator.getChildAt(0); //indicator is a horizontal scroll view, there will be only one root layout
以下是我的自定义字体的作用:
public static void setTypeface(TextView view, String font) {
Typeface typeface = Typeface.createFromAsset(view.getContext().getAssets(), BASE_PATH + font);
view.setTypeface(typeface);
}
一个更好的OOD实现是通过创建新接口来修改库:
public interface FontPagerAdapter {
/**
* Get the fonts to set.
*/
Typeface getCustomFont();}
并在TabPageIndicator.java类中添加一个新属性:
private Typeface customTypeFace;
将通过声明以下内容在notifyDataSetChanged()方法中设置:
if (adapter instanceof FontPagerAdapter) {
FontPagerAdapter fontAdapter = (FontPagerAdapter)adapter;
customTypeFace = fontAdapter.getCustomFont();
}
稍后,您可以通过在addTab方法中编程设置字体来更改字体,只需添加:
if (customTypeFace != null) {
tabView.setTypeface(customTypeFace);
}
最后,在将使用库的适配器中,您需要实现此接口,然后重写该方法:
@Override
public Typeface getCustomFont() {
Typeface font = Typeface.createFromAsset(context.getAssets(),"fonts/PoetsenOne-Regular.ttf");
return font;
}
请参阅我在此处发布的解决方案:
if (customTypeFace != null) {
tabView.setTypeface(customTypeFace);
}
@Override
public Typeface getCustomFont() {
Typeface font = Typeface.createFromAsset(context.getAssets(),"fonts/PoetsenOne-Regular.ttf");
return font;
}