android - 在运行时设置字体,TextView

标签 android fonts textview

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

我创建了一个 TextView

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

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

如何做到这一点?

最佳答案

在运行时设置内置字体:

  • 首先,改变字体,a Typeface 使用类。

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

  • at Design-Time,设置字体,使用android:typeface="serif"

例如:

<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 中。例如,您可以创建 assets/fonts/ 并将您的 TTF 文件放入其中:

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

关于android - 在运行时设置字体,TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3483110/

相关文章:

android - 如何使用新的 Dropbox API 下载文件?

css - 文本每个字符前进几个像素

java - Android AutoCompleteTextView 显示错误的建议

android - 单击按钮时旋转一定角度

android - 检查 Intent.ACTION_VIEW 上的用户操作

android - SherlockListFragment 缓存颜色提示

java - 每个滚动条上的项目都会调用 PagerAdapter 实例化项目

fonts - 如何使用 Swift 创建属性字符串?

ios - Xamarin 无法识别已安装的字体

android - 如何替换 fragment 中textView的文本