私は ViewPagerIndicator を使用していますが、運がなかったので、ここで質問します。TabPageIndicator のタブのフォントを変更する方法はありますか?
質問する
1755 次
3 に答える
5
これを行うために他のサードパーティのライブラリを使用する必要はありません。次のようにTabViewクラス(TabPageIndicator内)を変更できます(これは、アプリ全体で同じフォントが必要な場合)。
private class TabView extends TextView {
private int mIndex;
public TabView(Context context) {
super(context, null, R.attr.vpiTabPageIndicatorStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Re-measure if we went beyond our maximum size.
if (mMaxTabWidth > 0 && getMeasuredWidth() > mMaxTabWidth) {
super.onMeasure(MeasureSpec.makeMeasureSpec(mMaxTabWidth, MeasureSpec.EXACTLY), heightMeasureSpec);
}
}
public int getIndex() {
return mIndex;
}
public TabView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public TabView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TabView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/myFont.ttf"); // setting a custom TypeFace
setTypeface(tf);
}
于 2013-01-09T22:22:02.593 に答える
3
サードパーティのライブラリを使用したり、ライブラリを変更したりする必要はありませんViewPagerIndicator
。
TabPageIndicator
ViewGroup
tab を含む単一の子があるView
ため、このコードを使用してフォントを変更できます。
ViewGroup vg = (ViewGroup) indicator.getChildAt(0);
int vgChildCount = vg.getChildCount();
for (int j = 0; j < vgChildCount; j++) {
View vgChild = vg.getChildAt(j);
if (vgChild instanceof TextView) {
((TextView) vgChild).setTypeface(YOUR_FONT);
}
}
于 2014-02-14T18:01:40.367 に答える
0
そこで、Oak というライブラリ (TextViewWithFont がある場所) を使用して、このメソッドに PagerIndicator を渡すという、力ずくでそれを機能させる方法を試しました。
private static void changeFonts(ViewGroup root, Activity a, String typeface) {
Typeface tf = TextViewWithFont.getStaticTypeFace(a, typeface);
for (int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
} else if (v instanceof ViewGroup) {
changeFonts((ViewGroup) v, a, typeface);
}
}
}
public static void setTabPageIndicatorFont(ViewGroup root, Activity a) {
changeFonts(root, a, "DINCondBold.otf");
}
興味のある方はオーク: http://willowtreeapps.github.com/OAK/
誰かがそれについてもっと良い方法を持っているなら、私はセカンドオピニオンを求めたい.
于 2013-01-09T16:34:59.390 に答える