如何设置运行时创建的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/