在运行时设置字体,Textview

问题描述 投票:36回答:7

如何将字体设置为在运行时创建的textview?

我创建了textview

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

就像我可以改变大小 我想将字体样式设置为“Verdana”。

这该怎么做??尊重希什尔

android fonts textview
7个回答
65
投票

在运行时设置内置字体:

  • 首先,要改变Font-face,使用 Typeface 类。
  • 现在,at Run-Time,设置font-face,使用Java代码中的setTypeface(Typeface)
  • at Design-Time,设置font-face,使用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应用程序中设置自定义字体

要执行此操作,只需在项目根目录中创建资产/文件夹,并将字体(以TrueType或TTF格式)放入资产中。例如,您可以创建assets/fonts/并将您的TTF文件放在那里:

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

4
投票

您可以在资产文件夹中包含.ttf字体。假设字体的名称是“default.ttf”,您现在必须写下2行代码

TextView text = new TextView(this);
text.setTypeface(Typeface.createFromAsset(getAssets(), "default.ttf"));

您还必须小心,因为不同的字体有不同的大小。您可能需要将大小设置为:

text.setTextSize(20);

2
投票

这是一个小实用类

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);
    }

}

对于像适配器生成子节点的Spinners或ListViews(即任何类型的AdapterView),您需要在适配器的View(或类似)方法中设置每个项目getView的字体。这是因为可以根据需要创建视图,因此在Typeface中设置onCreate将无法正常工作。


2
投票

随着在Android 8.0中引入Fonts in XML(从API版本14向后兼容),它很容易从xml本身设置字体。

从android文档:

Android 8.0(API级别26)引入了一项新功能,即XML中的字体,它允许您将字体用作资源。您可以在res / font /文件夹中添加字体文件以将字体捆绑为资源。这些字体在您的R文件中编译,并在Android Studio中自动提供。您可以借助新的资源类型字体访问字体资源。例如,要访问字体资源,请使用@ font / myfont或R.font.myfont。

首先在名为font的res文件夹中创建一个Android资源目录 将.ttf字体文件添加到该目录,然后创建字体系列

Create a font family

字体系列是一组字体文件及其样式和重量详细信息。在Android中,您可以创建一个新的字体系列作为XML资源,并将其作为单个单元访问,而不是将每个样式和权重作为单独的资源引用。通过这样做,系统可以根据您尝试使用的文本样式选择正确的字体。

要创建字体系列,请在Android Studio中执行以下步骤:

  1. 右键单击字体文件夹,然后转到“新建”>“字体资源文件”。将出现“新建资源文件”窗口。
  2. 输入文件名,然后单击“确定”。新的字体资源XML在编辑器中打开。
  3. 将每个字体文件,样式和权重属性括在<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"/>

1
投票

你需要使用Typeface

  1. 将您希望使用的字体添加到项目中作为资产。
  2. 使用该字体创建字体对象: Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/MyFont.ttf");
  3. 将字体设置为您要自定义的对象: TextView myTextView = (TextView)findViewById(R.id.my_text_view); myTextView.setTypeface(myFont);

0
投票

动态地你可以使用这个在xml中设置类似于android:fontFamily的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));

这些是使用的默认字体系列列表,通过替换双引号字符串“sans-serif-medium”来使用任何一个

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中


-2
投票

您可以使用以下代码在运行时将所有文本设置为特定字体。只需在Activity setViewGroupFont方法结束时或在动态创建新视图时调用onCreate方法:

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
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.