android - android 中的自定义字体(适用于整个应用程序)

标签 android performance fonts

<分区>

我有这个 android 代码,其中包含许多类,每个类都有不同的 View 。我们可以转到设置并根据所选字体更改其字体。目前只有预装的 android 字体可用。有没有办法稍微调整我的代码,以便我可以添加一个 .ttf 文件并将其作为字体选项提供。 我不想对代码进行大的更改。

最佳答案

您可以使用 typeface 为 textview 中的文本设置自定义字体。因此,无论何时您需要为您的 TextView 自定义字体,您都可以使用以下内容。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#FFF"
          />
 </LinearLayout>

代码:

public class MainActivity extends Activity {

private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.text);
    Button b= (Button) findViewById(R.id.button1);
    b.setOnClickListener( new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            text.setText("This is a custom toast");
            Typeface typeFace =  Typeface.createFromAsset(getAssets(),"fonts/kn.ttf");
            text.setTypeface(typeFace);

        }

    });

}
}

enter image description here

关于android - android 中的自定义字体(适用于整个应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15991521/

相关文章:

android - 在我的应用程序中固定公钥

Mysql:唯一索引=大型数据集的性能特征?

java - Activity 关闭时停止方法重复

java - Android:如何通过按钮启动 Activity ?

matlab - Matlab中的对象数组效率

fonts - 我需要购买字体许可证才能在 iPhone 游戏中使用它吗?

pdf - 当我使用 itext 调用 BaseFont.createFont() 时,如何知道正确的编码?

java - OpenJDK 11 java.lang.ClassCastException : class sun. font.CompositeFont 无法转换为类 sun.font.PhysicalFont

android - 更新到 New Android Studio 3.2 后出现错误

Python:以优雅的方式(代码保存)改进一个函数以避免多个语句