简体   繁体   中英

Custom Font not working in Fragment Android Studio

I wanted to make a custom font textview.

I've got in FragEarnCredits.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (aiView == null) {
            aiView = inflater.inflate(R.layout.fragment_earn_credits, container, false);
        }


        t = (TextView) aiView.findViewById(R.id.logo);
        Typeface myCustomFont=Typeface.createFromAsset(aiContext.getAssets(),"fonts/bebas.otf");
        t.setTypeface(myCustomFont);

In fragment_earn_credits.xml :

<TextView
    android:id="@+id/logo"
    android:text="Fashion Wallet"
    android:gravity="center"
    android:textSize="40dp"
    android:textColor="@color/md_brown_700"
    android:layout_marginTop="100dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
  />

And I'm getting null error. Why? Please help me guys!

#EDIT

I tried to change:

 Typeface myCustomFont=Typeface.createFromAsset(aiContext.getAssets(),"fonts/bebas.otf");

to

Typeface myCustomFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/bebas.otf");

And 0 errors but there is not changing font.

Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/customfont.ttf");

The context you are sending in the fragment class, you have to use your actual context.

Hope it helps! Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM