Android: 想要为整个应用设置自定义字体,而不是在运行时设置。

9 浏览
0 Comments

Android: 想要为整个应用设置自定义字体,而不是在运行时设置。

在应用程序的每个控件中设置任何自定义字体是否可能?并且不一定是在运行时?(即,如果可能的话,可以从xml中设置,或者只在整个应用程序的JAVA文件中设置一次)

我可以使用以下代码为一个控件设置字体。

public static void setFont(TextView textView) {
    Typeface tf = Typeface.createFromAsset(textView.getContext()
            .getAssets(), "fonts/BPreplay.otf");
    textView.setTypeface(tf);
}

这段代码的问题是它应该为每个控件调用。我希望只调用一次这个或类似的方法,或者如果可能的话在xml中设置属性。这是可能的吗?

0
0 Comments

问题原因:这个问题的原因是想要为整个应用程序设置自定义字体,而不是在运行时设置。

解决方法:通过创建一个扩展TextView的自定义小部件,可以很容易地通过XML实现这一点。

首先,在res/values/目录下创建一个名为attrs.xml的文件,文件内容如下:


    
        
    

然后,创建自定义小部件TypefacedTextView:

package your.package.widget;
public class TypefacedTextView extends TextView {
    public TypefacedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        if (isInEditMode()) {
            return;
        }
        
        TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
        String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface);
        styledAttrs.recycle();
        
        if (fontName != null) {
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
            setTypeface(typeface);
        }
    }
}

以上代码将从assets/文件夹中读取字体。在这个示例中,假设assets文件夹中有一个名为"custom.ttf"的文件。

最后,在XML中使用这个小部件:


注意:在Eclipse的布局编辑器中无法看到自定义字体。这就是为什么代码中使用了isInEditMode()检查的原因。但如果运行应用程序,自定义字体将正常工作。

以上是问题的原因和解决方法,是一个非常简单易用的代码集成。

0
0 Comments

问题出现的原因:希望在整个应用程序中设置自定义字体,而不是在运行时设置。

解决方法:创建一个自定义的TextView类,命名为RobotoTextView,并在其中设置字体属性。该类继承自Android的TextView类,通过解析属性文件和在资源文件中设置字体样式,实现了在整个应用程序中设置自定义字体。

具体实现过程如下:

1. 在attr.xml文件中定义字体属性:




    


    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


2. 创建RobotoTextView.java文件,定义一个名为RobotoTextView的自定义TextView类。在该类中,通过解析属性文件和在资源文件中设置字体样式,实现了在整个应用程序中设置自定义字体。

public class RobotoTextView extends TextView {
    /*
     * Permissible values ​​for the "typeface" attribute.
     */
    private final static int ROBOTO_THIN = 0;
    private final static int ROBOTO_THIN_ITALIC = 1;
    private final static int ROBOTO_LIGHT = 2;
    private final static int ROBOTO_LIGHT_ITALIC = 3;
    private final static int ROBOTO_REGULAR = 4;
    private final static int ROBOTO_ITALIC = 5;
    private final static int ROBOTO_MEDIUM = 6;
    private final static int ROBOTO_MEDIUM_ITALIC = 7;
    private final static int ROBOTO_BOLD = 8;
    private final static int ROBOTO_BOLD_ITALIC = 9;
    private final static int ROBOTO_BLACK = 10;
    private final static int ROBOTO_BLACK_ITALIC = 11;
    private final static int ROBOTO_CONDENSED = 12;
    private final static int ROBOTO_CONDENSED_ITALIC = 13;
    private final static int ROBOTO_CONDENSED_BOLD = 14;
    private final static int ROBOTO_CONDENSED_BOLD_ITALIC = 15;
    
    /**
     * List of created typefaces for later reused.
     */
    private final static SparseArray mTypefaces = new SparseArray(16);
    
    /**
     * Simple constructor to use when creating a view from code.
     *
     * @param context The Context the view is running in, through which it can
     *                access the current theme, resources, etc.
     */
    public RobotoTextView(Context context) {
        super(context);
    }
    
    /**
     * Constructor that is called when inflating a view from XML. This is called
     * when a view is being constructed from an XML file, supplying attributes
     * that were specified in the XML file. This version uses a default style of
     * 0, so the only attribute values applied are those in the Context's Theme
     * and the given AttributeSet.
     * 

*

* The method onFinishInflate() will be called after all children have been * added. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. * #RobotoTextView(Context, AttributeSet, int) */ public RobotoTextView(Context context, AttributeSet attrs) { super(context, attrs); parseAttributes(context, attrs); } /** * Perform inflation from XML and apply a class-specific base style. This * constructor of View allows subclasses to use their own base style when * they are inflating. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. * @param defStyle The default style to apply to this view. If 0, no style * will be applied (beyond what is included in the theme). This may * either be an attribute resource, whose value will be retrieved * from the current theme, or an explicit style resource. * #RobotoTextView(Context, AttributeSet) */ public RobotoTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); parseAttributes(context, attrs); } /** * Parse the attributes. * * @param context The Context the view is running in, through which it can access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. */ private void parseAttributes(Context context, AttributeSet attrs) { TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.RobotoTextView); int typefaceValue = values.getInt(R.styleable.RobotoTextView_typeface, 0); values.recycle(); setTypeface(obtaintTypeface(context, typefaceValue)); } /** * Obtain typeface. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param typefaceValue values ​​for the "typeface" attribute * @return Roboto { Typeface} * @throws IllegalArgumentException if unknown `typeface` attribute value. */ private Typeface obtaintTypeface(Context context, int typefaceValue) throws IllegalArgumentException { Typeface typeface = mTypefaces.get(typefaceValue); if (typeface == null) { typeface = createTypeface(context, typefaceValue); mTypefaces.put(typefaceValue, typeface); } return typeface; } /** * Create typeface from assets. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param typefaceValue values ​​for the "typeface" attribute * @return Roboto { Typeface} * @throws IllegalArgumentException if unknown `typeface` attribute value. */ private Typeface createTypeface(Context context, int typefaceValue) throws IllegalArgumentException { Typeface typeface; switch (typefaceValue) { case ROBOTO_THIN: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Thin.ttf"); break; case ROBOTO_THIN_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-ThinItalic.ttf"); break; case ROBOTO_LIGHT: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf"); break; case ROBOTO_LIGHT_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-LightItalic.ttf"); break; case ROBOTO_REGULAR: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf"); break; case ROBOTO_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Italic.ttf"); break; case ROBOTO_MEDIUM: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"); break; case ROBOTO_MEDIUM_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-MediumItalic.ttf"); break; case ROBOTO_BOLD: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); break; case ROBOTO_BOLD_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldItalic.ttf"); break; case ROBOTO_BLACK: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"); break; case ROBOTO_BLACK_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BlackItalic.ttf"); break; case ROBOTO_CONDENSED: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Condensed.ttf"); break; case ROBOTO_CONDENSED_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-CondensedItalic.ttf"); break; case ROBOTO_CONDENSED_BOLD: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensed.ttf"); break; case ROBOTO_CONDENSED_BOLD_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensedItalic.ttf"); break; default: throw new IllegalArgumentException("Unknown `typeface` attribute value " + typefaceValue); } return typeface; } }

3. 在布局文件中使用自定义的RobotoTextView,设置typeface属性为所需的字体样式,如下所示:


通过以上步骤,我们可以在整个应用程序中设置自定义字体,而不是在运行时设置。

0
0 Comments

在Android应用程序中设置自定义字体,而不是在运行时设置字体。

问题出现的原因是无法在XML中设置自定义字体,且在代码中设置字体的方法不够简洁。可以通过创建一个自定义的View类来解决这个问题,并在其中重写相应的方法。

解决方法如下:

1. 创建一个新的类,继承想要自定义的View类(例如,想要自定义字体的Button,就继承Button类)。

2. 在该类中添加一个构造方法和一个带有AttributeSet参数的构造方法,并在后者中调用一个解析属性的方法。

3. 在res/values文件夹下创建一个XML文件(例如attrs.xml),定义自定义属性。

4. 在自定义View类中的解析属性的方法中,通过获取TypedArray对象获取自定义属性的值,并根据不同的值设置不同的字体。

5. 在XML布局文件中使用自定义View类,并通过自定义属性来设置字体。

以上方法可以实现在整个应用程序中设置自定义字体的效果。

注意:如果要在样式中使用自定义字体,不需要添加xmlns:custom属性。只需使用属性的名称即可。

如果想要根据语言环境设置字体,可以通过判断语言环境的方式来选择不同的字体文件。

以上就是解决问题的方法,希望对你有所帮助!

0