在viewpagerindicator中使用自定义字体

时间:2013-07-04 07:31:59

标签: android viewpagerindicator custom-font

在我的项目中,我使用的是字体android:fontFamily =“sans-serif-light”,并且工作正常。

我也在使用库viewpagerindicator。我想在viewpagerindicator中使用字体android:fontFamily =“sans-serif-light”,但是找不到怎么做

我已尝试在android:fontFamily = "sans-serif-light"中使用<com.viewpagerindicator.TitlePageIndicator ...并使用风格,但没有成功。

我也尝试过:

PageIndicator mIndicator = (TitlePageIndicator) findViewById (R.id.indicator);
Typeface myTypeface = Typeface.createFromAsset (getAssets (), "fonts / Roboto-Light.ttf");
mIndicator.setTypeface (myTypeface);

但这不起作用..

我感谢任何帮助。

谢谢和问候

3 个答案:

答案 0 :(得分:10)

如果我没错,你想在视图寻呼机指示器中更改标题字体,

我改变了库以实现这一目标, 对于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上设置typeFace,如下所示:

mTabPageIndicator = (TabPageIndicator) findViewById(R.id.tab_page_indicator);
mTabPageIndicator.setTypeFace(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/custome_font.ttf");

答案 1 :(得分:0)

我正在使用ViewPageIndicator库,这是我的工作方式(索引是视图页面指示器中的标签索引):

private void changeTabText(int index, boolean on){
    if(tabLayout.getChildCount()-1 >= index){
        TextView tv = (TextView)tabLayout.getChildAt(index);
        tv.setTextColor(getResources().getColor(on? android.R.color.black : R.color.light_grey));
        CustomFont.setTypeface(tv, CustomFont.NORMAL);
    }
}

以下是我如何获得tablayout:

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);
}

答案 2 :(得分:0)

更多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;
}
相关问题