Textview tv = new TextView(this);
tv.setTextSize(20);
我可以轻松地改变字体大小,现在我想将字体样式设置为“Verdana”。
如何做到这一点?
首先,为了更改字体,使用 Typeface 类。
现在,在运行时
,可以使用 Java 代码中的 setTypeface(Typeface)
来设置字体。
在设计时
,可以使用 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" />
要实现这个目标,只需在项目根目录下创建一个名为assets/的文件夹,并将字体(以TrueType或TTF格式)放入其中。例如,可以创建assets/fonts/
文件夹并将TTF文件放入其中:
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
tv.setTypeface(face);
TextView text = new TextView(this);
text.setTypeface(Typeface.createFromAsset(getAssets(), "default.ttf"));
同时需要注意的是,不同的字体具有不同的大小。您可能需要设置字体大小为:
text.setTextSize(20);
您可以使用存储在“res/font”字体文件夹中的字体,例如适用于API级别16及以上的版本。
Typeface typeface = ResourcesCompat.getFont(context, R.font.rubik_medium);
txtView.setTypeface(typeface);
你也可以使用
Typeface typeface = getResources().getFont(R.font.rubik_medium);
txtView.setTypeface(typeface);
但它只支持API级别26及以上。
在Android 8.0中(向后兼容API版本14),引入了XML中的字体,使得从xml本身设置字体变得非常容易。
根据Android文档:
Android 8.0(API级别26)引入了一个新功能,Fonts in XML,它允许您将字体用作资源。您可以将字体文件添加到res/font/文件夹中以将字体捆绑为资源。这些字体已编译在R文件中,并自动在Android Studio中提供。您可以借助新的资源类型font访问字体资源。例如,要访问字体资源,请使用@font/myfont或R.font.myfont。
首先,在res
文件夹中创建名为font
的Android Resource Directory,然后将.ttf字体文件添加到该目录中,接着创建字体族。
将每个字体文件、样式和权重属性封装在<font>
元素中。以下XML演示了如何在字体资源XML中添加与字体相关的属性:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/lobster_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/lobster_italic" />
</font-family>
然后使用以下代码在您的textView
中设置字体
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lobster"/>
通过使用以下方法,您可以动态地设置字体族类似于xml中的android:fontFamily:
For Custom font:
TextView tv = ((TextView) v.findViewById(R.id.select_item_title));
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/mycustomfont.ttf");
tv.setTypeface(face);
For Default font:
tv.setTypeface(Typeface.create("sans-serif-medium",Typeface.NORMAL));
FONT FAMILY TTF FILE
1 casual ComingSoon.ttf
2 cursive DancingScript-Regular.ttf
3 monospace DroidSansMono.ttf
4 sans-serif Roboto-Regular.ttf
5 sans-serif-black Roboto-Black.ttf
6 sans-serif-condensed RobotoCondensed-Regular.ttf
7 sans-serif-condensed-light RobotoCondensed-Light.ttf
8 sans-serif-light Roboto-Light.ttf
9 sans-serif-medium Roboto-Medium.ttf
10 sans-serif-smallcaps CarroisGothicSC-Regular.ttf
11 sans-serif-thin Roboto-Thin.ttf
12 serif NotoSerif-Regular.ttf
13 serif-monospace CutiveMono.ttf
"mycustomfont.ttf"是ttf文件。路径将位于src/assets/fonts/mycustomfont.ttf中。
public class TypefaceHelper {
public static void setViewGroupTypeface(ViewGroup container, Typeface typeface) {
final int children = container.getChildCount();
for (int i = 0; i < children; i++)
View child = container.getChildAt(i);
if (child instanceof TextView) {
setTextViewTypeface((TextView) child, typeface);
} else if (child instanceof ViewGroup) {
setViewGroupTypeface((ViewGroup) child, typeface);
}
}
}
public static void setTextViewTypeface(TextView textView, Typeface typeface) {
textView.setTypeface(typeface);
}
}
对于像Spinner
或ListView
这样的控件(即任何类型的AdapterView
),它们会从适配器中生成子项,因此您需要在适配器的getView
(或类似)方法中设置每个子项View
的字体类型。这是因为视图可能会根据需要创建,所以在onCreate
中设置Typeface
将无法正常工作。
styles.xml
中创建一个样式。<style name="YourFont">
<item name="android:fontFamily">@font/your_font</item>
</style>
然后您只需要使用以下方式添加样式
textView.setTextAppearance(context, R.style.YourFont);
您需要使用字体:
使用该字体创建Typeface对象:
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/MyFont.ttf");
将字体设置为您想要自定义的对象:
TextView myTextView = (TextView)findViewById(R.id.my_text_view);
myTextView.setTypeface(myFont);
您可以使用以下代码在运行时将所有文本设置为特定字体。只需在您的Activity的onCreate
方法的末尾或每当您动态创建新视图时调用setViewGroupFont
方法:
private static final String FONT_NAME = "fonts/Roboto-Regular.ttf";
private static Typeface m_font = null;
public static Typeface getFont(Context p_context)
{
if (null == m_font && null != p_context)
{
m_font = Typeface.createFromAsset(p_context.getApplicationContext().getAssets(), FONT_NAME);
}
return m_font;
}
public static void setViewGroupFont(ViewGroup p_viewGroup)
{
if (null != p_viewGroup)
{
for (int currChildIndex = 0; currChildIndex < p_viewGroup.getChildCount(); currChildIndex++)
{
View currChildView = p_viewGroup.getChildAt(currChildIndex);
if (ViewGroup.class.isInstance(currChildView))
{
setViewGroupFont((ViewGroup) currChildView);
}
else
{
try
{
Method setTypefaceMethod = currChildView.getClass().getMethod("setTypeface", Typeface.class);
setTypefaceMethod.invoke(currChildView, getFont(p_viewGroup.getContext()));
}
catch (NoSuchMethodException ex)
{
// Do nothing
}
catch (Exception ex)
{
// Unexpected error setting font
}
}
}
}
}
TextView
或ViewGroup
的实例。 - Joseph EarlSpinner
继承自 ViewGroup
,而 Button
继承自 TextView
,因此像我之前所说的那样,检查它是否是 TextView
或 ViewGroup
的实例通常就足够了。 - Joseph Earl