Android 如何将ttf文件用于按钮';s文本?
如何将ttf文件用于按钮文本?这是否意味着您必须更改按钮的字体Android 如何将ttf文件用于按钮';s文本?,android,Android,如何将ttf文件用于按钮文本?这是否意味着您必须更改按钮的字体 Context context = textView.getContext(); helvetica_normal = Typeface.createFromAsset(context.getAssets(), "fonts/helvetica.ttf"); textView.setTypeface(helvetica_normal); 这是将文本视图的字体更改为android默认字体为he
Context context = textView.getContext();
helvetica_normal = Typeface.createFromAsset(context.getAssets(),
"fonts/helvetica.ttf");
textView.setTypeface(helvetica_normal);
这是将文本视图的字体更改为android默认字体为helvetica的代码。
由于Button扩展了文本视图,您可以直接使用此代码而无需任何修改,例如Button、Text view和if I'm not Error微调器
注意:假设您已将字体的ttf文件复制到Assets文件夹。这是否意味着您必须更改按钮的字体
Context context = textView.getContext();
helvetica_normal = Typeface.createFromAsset(context.getAssets(),
"fonts/helvetica.ttf");
textView.setTypeface(helvetica_normal);
这是将文本视图的字体更改为android默认字体为helvetica的代码。
由于Button扩展了文本视图,您可以直接使用此代码而无需任何修改,例如Button、Text view和if I'm not Error微调器
注意:假设您已将字体的ttf文件复制到Assets文件夹中。在整个活动中应用该文件的一个简便例程是在onCreate()中的setContentView()之后调用以下命令。在Adapter.getView()中也很有用
静态字体=null;
静态字体(上下文、视图组vg)
{
如果(字体==null)
typeface=typeface.createFromAsset(context.getAssets(),“ourfont.ttf”);
int cnt=vg.getChildCount();
对于(int i=0;i来说,在整个活动中应用这一点的一个简便例程是在onCreate()中的setContentView()之后调用以下内容。在Adapter.getView()中也很有用
静态字体=null;
静态字体(上下文、视图组vg)
{
如果(字体==null)
typeface=typeface.createFromAsset(context.getAssets(),“ourfont.ttf”);
int cnt=vg.getChildCount();
对于(int i=0;i基于Prahams Post,我只是在我的类中使用了代码片段。我刚刚创建了两个方法,它们完成了所有工作,因此您只需将每个元素传递给该方法,它将以特定的ttf字体创建
我在课程开始时创建了一个变量
Typeface typeface;
然后必须将实例化的元素传递给方法
setFont(anyElement);
这里有两个方法可以完成这项工作。在“setFont”中,第二个方法“getFontFromAsset”将被调用,因此如果需要,可以在一个位置更改字体
private void setFont(TextView element){
Context context = element.getContext();
element.setTypeface(getFontFromAsset(context));
}
private Typeface getFontFromAsset(Context context){
typeface = Typeface.createFromAsset(context.getAssets(), "beware.ttf");
return typeface;
}
或者,为了使其更有用,将其源代码输出到一个新类。而不是只有一个地方可以设置字体,您只需在活动中初始化该类,并通过FontClass方法传递元素
这是我的课
package xandru.cea;
import android.content.Context;
import android.graphics.Typeface;
import android.widget.TextView;
public class FontClass {
Typeface typeface;
public FontClass(){
return;
}
public void setFont(TextView element){
Context context = element.getContext();
element.setTypeface(getFontFromAsset(context));
}
private Typeface getFontFromAsset(Context context){
typeface = Typeface.createFromAsset(context.getAssets(), "beware.ttf");
return typeface;
}
}
而不是初始化类:
FontClass fontClass = new FontClass();
并使元素通过:
fontClass.setFont(anyElement);
基于Prahams Post,我只是在我的类中使用了代码片段。我刚刚创建了两个方法来完成所有工作,因此您只需将每个元素传递给该方法,它将以特定的ttf字体创建
我在课程开始时创建了一个变量
Typeface typeface;
然后必须将实例化的元素传递给方法
setFont(anyElement);
这里有两个方法可以完成这项工作。在“setFont”中,第二个方法“getFontFromAsset”将被调用,因此如果需要,可以在一个位置更改字体
private void setFont(TextView element){
Context context = element.getContext();
element.setTypeface(getFontFromAsset(context));
}
private Typeface getFontFromAsset(Context context){
typeface = Typeface.createFromAsset(context.getAssets(), "beware.ttf");
return typeface;
}
或者,为了使其更有用,将其源代码输出到一个新类。而不是只有一个地方可以设置字体,您只需在活动中初始化该类,并通过FontClass方法传递元素
这是我的课
package xandru.cea;
import android.content.Context;
import android.graphics.Typeface;
import android.widget.TextView;
public class FontClass {
Typeface typeface;
public FontClass(){
return;
}
public void setFont(TextView element){
Context context = element.getContext();
element.setTypeface(getFontFromAsset(context));
}
private Typeface getFontFromAsset(Context context){
typeface = Typeface.createFromAsset(context.getAssets(), "beware.ttf");
return typeface;
}
}
而不是初始化类:
FontClass fontClass = new FontClass();
并使元素通过:
fontClass.setFont(anyElement);
您可以在此处看到一个示例:
只需将customFont更改为您的按钮。您可以在此处看到一个示例:
只需将自定义字体更改为您的按钮