在运行时设置字体,TextView [英] Set font at runtime, TextView

查看:28
本文介绍了在运行时设置字体,TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置运行时创建的TextView的字体?

How to set the font of a TextView created at runtime?

我创建了一个 TextView

I created a TextView

Textview tv = new TextView(this);      
tv.setTextSize(20);

我可以轻松更改大小,现在我想将字体样式设置为Verdana".

I can easily change the size, now I'd like to set font style to "Verdana".

如何做到这一点?

推荐答案

在运行时设置内置字体:

  • 首先,要改变字体,一个 字体 使用了类.

    To set In-built Font at Run-Time:

    • First of all, To Change Font-face, a Typeface class is used.

      现在,在运行时,设置字体,使用setTypeface(Typeface) 来自 Java 代码

      Now, at Run-Time, to set the font-face, Use setTypeface(Typeface) from the Java code

      例如:

      <TextView android:text="@+id/TextView01"
       android:id="@+id/TextView01"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textSize="30px"
       android:textStyle="italic"
       android:typeface="serif" />
      

      <小时>

      在您的 Android 应用程序中设置自定义字体

      为此,只需在项目根目录中创建一个 assets/文件夹,并将您的字体(以 TrueType 或 TTF 形式)放入资产中.例如,您可以创建 assets/fonts/ 并将您的 TTF 文件放在那里:


      To set Custom font(s) in your Android application

      To do this, simply create an assets/ folder in the project root, and put your fonts (in TrueType, or TTF, form) in the assets. You might, for example, create assets/fonts/ and put your TTF files in there:

        TextView tv=(TextView)findViewById(R.id.custom); 
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"); 
        tv.setTypeface(face); 
      

      这篇关于在运行时设置字体,TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆