简体   繁体   English

android:整个应用程序的字体家族

[英]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. 因此,我将所需的字体复制到assets/fonts/文件夹中,并进行了如下所示的编码。

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; 我的xml TextView代码;

  <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'). 错误:错误:不允许使用字符串类型(在“ id”中具有值“ 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. 在布局R.layout.activity_main ,没有ID为R.id.app_name TextView ,因此,当您在代码中初始化该TextView ,将引发错误。

Make sure you have such TextView in your layout. 确保您的布局中有这样的TextView

If you think you do have that TextView in the layout already, try and clean the project. 如果您认为布局中确实已有该TextView ,请尝试清理该项目。

Edit 编辑

Add the id as below: 如下添加ID:

android:id="@+id/app_name"

Also rename your font with lowercased ".ttf" extension, and the reference to it all the same. 还要用小写的“ .ttf”扩展名重命名字体,并对其引用全部相同。

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属性,例如:

<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 Android仅允许整数类型ID

You mention string type id thats your mistake 您提到了字符串类型ID,这就是您的错误

Change this Line 改变这条线

android:id="app_name"

to

android:id="@+id/app_name"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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