简体   繁体   中英

android: font family for whole app

I don't want to use the default font for my app.

I want to use my desired font.

Therefore, I copied my desired font in assets/fonts/ folder and coded as shown below.

However, an error occurs.

Please help me use the same font for the full app.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout ll = new LinearLayout(this);
    ll.setBackgroundResource(R.drawable.bg);
    this.setContentView(ll);
    TextView tv = (TextView) findViewById(R.id.app_name);
    Typeface face = Typeface.createFromAsset(getAssets(),
                    "fonts/TAU_MADN.TTF");
    tv.setTypeface(face);

UPDATE:

My xml TextView code;

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="app_name"
        android:textStyle="bold" />

Error:

error: Error: String types not allowed (at 'id' with value 'app_name').

请确保正确指定了路径,例如区分大小写的东西,包括扩展名.TTF或.ttf(与字体文件夹中的名称相同),包括文件夹名称“ FONTS”!

Your error has nothing to do with the loading of the custom font as an asset.

In your layout R.layout.activity_main there is no TextView with id: R.id.app_name , therefore when you initialize that TextView in your code the error will thrown.

Make sure you have such TextView in your layout.

If you think you do have that TextView in the layout already, try and clean the project.

Edit

Add the id as below:

android:id="@+id/app_name"

Also rename your font with lowercased ".ttf" extension, and the reference to it all the same.

Typeface face = Typeface.createFromAsset(getAssets(),
                    "fonts/TAU_MADN.ttf"); // lowercased ".ttf" reference

See here for more info on your issue.

May this help you:

Change android:id attribute in your TextView Like:

<TextView
        android:id="@+id/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

And also change this line in your code:

Typeface face = Typeface.createFromAsset(getAssets(),
                    "fonts/TAU_MADN.ttf");

Android allows integer type ids only

You mention string type id thats your mistake

Change this Line

android:id="app_name"

to

android:id="@+id/app_name"

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