在运行时设置字体,TextView

编程入门 行业动态 更新时间:2024-10-26 08:28:21
本文介绍了在运行时设置字体,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

本文发布于:2023-06-10 22:50:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/620769.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字体   TextView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!