从XML自定义TrueType字体只 [英] Custom TrueType font from xml only

查看:99
本文介绍了从XML自定义TrueType字体只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过自定义字体的TextView(或它的子类),仅使用XML,而不提供任何Java code这样

 字样TF = Typeface.createFromAsset(getAssets(),字体/ CustomFont.ttf);
 

解决方案

这是不可能做到这一点的单纯从XML 的,但你的可以的创建自定义视图和引用从XML。这样,你只需要编写code一次,让您可以回收利用的各种布局。

例如,声明类 FontTextView

 包com.example;

进口android.content.Context;
进口android.util.AttributeSet;
进口android.widget.TextView;

公共类FontTextView扩展TextView的{

    / **
     *请注意,产生从code中的类时,你需要
     *手动调用setCustomFont()。
     * /
    公共FontTextView(上下文的背景下){
        超(上下文);
    }

    公共FontTextView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        setCustomFont(这一点,ATTRS);
    }

    公共FontTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        setCustomFont(这一点,ATTRS);
    }

    私人无效setCustomFont(上下文的背景下,ATTRS的AttributeSet){
        如果(isInEditMode()){
            //忽略,如果在Eclipse
            返回;
        }
        字符串字体=myDefaultFont.ttf;
        如果(ATTRS!= NULL){
            //查找任何布局定义的属性
            TypedArray A = obtainStyledAttributes(ATTRS,
                    R.styleable.FontTextView);
            的for(int i = 0; I< a.getIndexCount();我++){
                INT ATTR = a.getIndex(ⅰ);
                开关(ATTR){
                案例R.styleable.FontTextView_customFont:
                    字体= a.getString(ATTR,0);
                    打破;
                }
            }
            a.recycle();
        }
        字样TF = NULL;
        尝试 {
            TF = Typeface.createFromAsset(getAssets(),字体);
        }赶上(例外五){
            Log.e(无法获取的字样:+ e.getMessage());
        }
        setTypeface(TF);
    }

}
 

定义中的 RES /价值/ attrs.xml属性

 < XML版本=1.0编码=UTF-8&GT?;
<资源>

    <申报,设置样式名称=FontTextView>
        < attr指示NAME =CustomFont的格式=字符串/>
    < /申报,设置样式>

< /资源>
 

使用它的布局:

  1. 声明命名空间:

     的xmlns:定制=htt​​p://schemas.android.com/apk/res/com.example
     

  2. 使用了 FontTextView

     < com.example.FontTextView
        机器人:ID =@ + ID /简介
        CustomFont的=myCustomFont.ttf
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=世界,你好! />
     

Is there any way to pass custom font for TextView (or subclasses of it) using only xml without providing any java code like this

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/CustomFont.ttf");

解决方案

It is not possible to do it purely from XML, but you can create a custom view and reference that from XML. This way you will only need to write the code once, allowing you to recycle it in various layouts.

For instance, declare the class FontTextView:

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class FontTextView extends TextView {

    /** 
     *  Note that when generating the class from code, you will need
     *  to call setCustomFont() manually.
     */
    public FontTextView(Context context) {
        super(context);
    }

    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(this, attrs);
    }

    public FontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(this, attrs);
    }

    private void setCustomFont(Context context, AttributeSet attrs) {
        if (isInEditMode()) {
            // Ignore if within Eclipse
            return;
        }
        String font = "myDefaultFont.ttf";
        if (attrs != null) {
            // Look up any layout-defined attributes
            TypedArray a = obtainStyledAttributes(attrs,
                    R.styleable.FontTextView);
            for (int i = 0; i < a.getIndexCount(); i++) {
                int attr = a.getIndex(i);
                switch (attr) {
                case R.styleable.FontTextView_customFont:
                    font = a.getString(attr, 0);
                    break;
                }
            }
            a.recycle();
        }
        Typeface tf = null;
        try {
            tf = Typeface.createFromAsset(getAssets(), font);
        } catch (Exception e) {
            Log.e("Could not get typeface: " + e.getMessage());
        }
        setTypeface(tf);
    }

}

Define the attribute in res/values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="FontTextView">
        <attr name="customFont" format="string" />
    </declare-styleable>

</resources>

Use it in a layout:

  1. Declare the namespace:

    xmlns:custom="http://schemas.android.com/apk/res/com.example"
    

  2. Use the FontTextView:

    <com.example.FontTextView
        android:id="@+id/introduction"
        customFont="myCustomFont.ttf"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello world!" />
    

这篇关于从XML自定义TrueType字体只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆