How to change the font pagertitlestrip

Typeface xyz=Typeface.createFromAsset(view.getContext().getAssets(), "fonts/xyz.ttf"); (TextView)abc.setTypeface(xyz); 

This is how you create and install a custom font.

I need to set the pagertitlestrip font / font, but there is no .setTypeFace function for .setTypeFace . Does anyone know how I can do this?

+4
source share
4 answers

PagerTitleStrip actually extends the ViewGroup , so you can simply subclass it and iterate over each of its children to find views that need to change their font:

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

The only downside is that it prevents Eclipse from displaying your layout.

+9
source

Apparently, I don't have enough reputation to comment on jburns20's answer, but I feel that my discovery will help those who are faced with its solution.

I noticed that there was a significant backwardness in my animation after I tried the jburns20 class. Through tracking, it became clear that the culprit repeatedly assigned a font resource. In my use case (and I'm sure many others), there is no need to repeatedly extract the font resource for each iteration.

Moving the instance before the loop solved the performance issues for me. For clarity, I reproduced my modification below, all credits should be sent to jburns20

 public class CustomFontPagerTitleStrip extends PagerTitleStrip { //Moved typeface instantiation here, only required once. Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/xyz.ttf"); 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); for (int i=0; i<this.getChildCount(); i++) { if (this.getChildAt(i) instanceof TextView) { ((TextView)this.getChildAt(i)).setTypeface(tf); } } } } 
+2
source

if a custom font puts it in the asset folder and changes it like that.

 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); } } 
+1
source

Although I think ViewPager supports android:textAppearance , you also cannot set the font.

Most likely, your best PagerTitleStrip copy the PagerTitleStrip code (in your SDK or available online ), reorganize your plug into your own package, and change it accordingly. Or see if the ViewPagerIndicator library has the one you like that allows you to customize the font.

0
source

Source: https://habr.com/ru/post/1439645/


All Articles