Set custom font for Android fragments

前端 未结 3 769
误落风尘
误落风尘 2020-12-15 18:15

I have been using Jake Wharton\'s ViewPagerIndicator and I am currently trying to implement a custom font on one of my fragments. I\'ve tried using this code:



        
相关标签:
3条回答
  • 2020-12-15 18:38

    Define typeface in yourViewHolder class`

    Typeface customFontBold= Typeface.createFromAsset(getActivity().getAssets(),"fonts/JosefinSans-Bold.ttf");
    

    like this.

    0 讨论(0)
  • 2020-12-15 18:47

    My Simple code is change method call "context" in the fragment with "getContext(); //is getContext and getActvity same in fragment??

    **result**.setText("Value Expenses = " +expenses);
    Typeface supercell = Typeface.createFromAsset(**getContext()**.getAssets(), "fonts/Supercell.ttf");**
    **result.setTypeface(supercell);**
    }catch (Exception e) 
    {
    Toast.makeText(getActivity(), "SUCCEED, VALUES RESULT", Toast.LENGTH_SHORT)
    .show();
    }
    break;
    
    0 讨论(0)
  • 2020-12-15 18:53

    You can try this

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle     savedInstanceState) {
           View v = inflater.inflate(R.layout.fragment_layout, container, false);
           TextView txt = (TextView) v.findViewById(R.id.Zipcode);
           Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/customfont.ttf");
           txt.setTypeface(font); 
           return v;
    }
    

    with this you can have the context in the scope of the fragment and get the view and the assets

    0 讨论(0)
提交回复
热议问题