Android studio – use the asset manager in the clip to set custom fonts

I'm rewriting one of my projects to use clips for tablets. I also decided to start using Android studio for conversion. It's very simple, but I encountered a very simple task about setting custom fonts

Let me start with these: custom fonts in Android and list of files in assets folder and its subfolders and set custom font for Android fragments

In eclipse, my class extends activity. I perform the following operations in oncreate without any problems. (there is a directory and file "assets / fut_r.ttf")

TextView tv=(TextView)findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getAssets(), "FUT_R.ttf"); 
tv.setTypeface(font);

Now try to convert this activity into actionbaractivity (fragment with V7 support library). I have changed the above code to this. (where the view is the phone inflatable layout with a FrameLayout)

TextView tv=(TextView) view.findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "FUT_R.ttf"); 
tv.setTypeface(font);

And it crashes at runtime with a lovely Java. Lang. runtimeException: Although I created the 'assets' directory in my new project and used fut in its root directory_ R. TTF, so the native font cannot be created

To confirm the asset manager, I tried the following code:

String[] f = null;
                try {
                    f = getActivity().getAssets().list("");
                }
                catch (IOException e){
                    e.printStackTrace();
                }
                for(String f1:f){
                    Log.i("names",f1);
                }

The following output is obtained:

07-26 07:40:40.134    2114-2114/com.myapp I/names: images
07-26 07:40:40.134    2114-2114/com.myapp I/names: sounds
07-26 07:40:40.134    2114-2114/com.myapp I/names: webkit

I'm confused because there are no these files and / or directories in my 'assets' directory or project. Obviously, the error is that the system can't find my font file. What did I do wrong? Any direction will be highly appreciated. I wasted too much time on this stupid question

thank you!

resolvent:

I finally stumbled upon this: load a simple text file in Android studio

It points out that the assets folder in Android studio has obviously been changed. It does not need to be in the normal position in the root application folder (the same as LIBS, SRC and ECT), but needs to be created under yourapp / SRC / main / assets. I hope this will help others. I wasted a lot of time!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>