Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android自定义字体-用于Actionbar、Toast和对话框等系统组件_Android_Fonts - Fatal编程技术网

Android自定义字体-用于Actionbar、Toast和对话框等系统组件

Android自定义字体-用于Actionbar、Toast和对话框等系统组件,android,fonts,Android,Fonts,我正在将应用程序翻译成不受支持的语言-设备上缺少字体。 所以我在应用程序中添加了字体,并更改了所有文本视图、编辑文本和按钮的字体。 我还可以在WebView中更改字体。 但现在我需要更改所有系统组件的字体: 操作栏: 标题下拉列表 菜单 ShareActionProvider 行动项目 溢出的ActionItems 对话 吐司 设置屏幕 是否有办法更改此组件的字体。 可能所有人都有一些文本视图-只是为了知道他们的id来找到他们 请不要在TextView上发布关于更改字体的答案 使用如下自定义

我正在将应用程序翻译成不受支持的语言-设备上缺少字体。 所以我在应用程序中添加了字体,并更改了所有文本视图、编辑文本和按钮的字体。 我还可以在WebView中更改字体。 但现在我需要更改所有系统组件的字体:

  • 操作栏:

    • 标题下拉列表
    • 菜单
    • ShareActionProvider
    • 行动项目
    • 溢出的ActionItems
  • 对话

  • 吐司
  • 设置屏幕
  • 是否有办法更改此组件的字体。 可能所有人都有一些文本视图-只是为了知道他们的id来找到他们


    请不要在TextView上发布关于更改字体的答案

    使用如下自定义字体创建视图

    @Override
    public void setTitleTypeface(Typeface TF) {}
    @Override
    public void setSubtitleTypeface(Typeface TF) {}
    
    FontManager.java

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    
    import android.content.Context;
    import android.content.res.XmlResourceParser;
    import android.graphics.Typeface;
    import android.view.InflateException;
    
    public class FontManager
    {
        //Making FontManager a singleton class
        private static class InstanceHolder
        {
            private static final FontManager INSTANCE = new FontManager();
        }
    
        public static FontManager getInstance()
        {
            return FontManager.InstanceHolder.INSTANCE;
        }
    
        private FontManager()
        {
        }
    
        // Different tags used in XML file.
        private static final String TAG_FAMILY = "family";
        private static final String TAG_NAMESET = "nameset";
        private static final String TAG_NAME = "name";
        private static final String TAG_FILESET = "fileset";
        private static final String TAG_FILE = "file";
    
        // Different styles supported.
        private static final String STYLE_BOLD = "-Bold.ttf";
        private static final String STYLE_ITALIC = "-Italic.ttf";
        private static final String STYLE_BOLDITALIC = "-BoldItalic.ttf";
    
        private class FontStyle
        {
            int style;
            Typeface font;
        }
    
        private class Font
        {
            // different font-family names that this Font will respond to.
            List<String> families;
    
            // different styles for this font.
            List<FontStyle> styles;
        }
    
        private List<Font> mFonts;
    
        //private boolean isFamilySet = false;
        private boolean isName = false;
        private boolean isFile = false;
    
        // Parse the resId and initialize the parser.
        public void initialize(Context context, int resId)
        {
            XmlResourceParser parser = null;
            try
            {
                parser = context.getResources().getXml(resId);
                mFonts = new ArrayList<Font>();
    
                String tag;
                int eventType = parser.getEventType();
    
                Font font = null;
    
                do
                {
                    tag = parser.getName();
    
                    switch ( eventType )
                    {
                        case XmlPullParser.START_TAG:
                            if ( tag.equals(TAG_FAMILY) )
                            {
                                // one of the font-families.
                                font = new Font();
                            }
                            else if ( tag.equals(TAG_NAMESET) )
                            {
                                // a list of font-family names supported.
                                font.families = new ArrayList<String>();
                            }
                            else if ( tag.equals(TAG_NAME) )
                            {
                                isName = true;
                            }
                            else if ( tag.equals(TAG_FILESET) )
                            {
                                // a list of files specifying the different styles.
                                font.styles = new ArrayList<FontStyle>();
                            }
                            else if ( tag.equals(TAG_FILE) )
                            {
                                isFile = true;
                            }
                            break;
    
                        case XmlPullParser.END_TAG:
                            if ( tag.equals(TAG_FAMILY) )
                            {
                                // add it to the list.
                                if ( font != null )
                                {
                                    mFonts.add(font);
                                    font = null;
                                }
                            }
                            else if ( tag.equals(TAG_NAME) )
                            {
                                isName = false;
                            }
                            else if ( tag.equals(TAG_FILE) )
                            {
                                isFile = false;
                            }
                            break;
    
                        case XmlPullParser.TEXT:
                            String text = parser.getText();
                            if ( isName )
                            {
                                // value is a name, add it to list of family-names.
                                if ( font.families != null )
                                    font.families.add(text);
                            }
                            else if ( isFile )
                            {
                                // value is a file, add it to the proper kind.
                                FontStyle fontStyle = new FontStyle();
                                fontStyle.font = Typeface.createFromAsset(context.getAssets(), text);
    
                                if ( text.endsWith(STYLE_BOLD) )
                                    fontStyle.style = Typeface.BOLD;
                                else if ( text.endsWith(STYLE_ITALIC) )
                                    fontStyle.style = Typeface.ITALIC;
                                else if ( text.endsWith(STYLE_BOLDITALIC) )
                                    fontStyle.style = Typeface.BOLD_ITALIC;
                                else
                                    fontStyle.style = Typeface.NORMAL;
    
                                font.styles.add(fontStyle);
                            }
                    }
    
                    eventType = parser.next();
    
                }
                while ( eventType != XmlPullParser.END_DOCUMENT );
    
            }
            catch ( XmlPullParserException e )
            {
                throw new InflateException("Error inflating font XML", e);
            }
            catch ( IOException e )
            {
                throw new InflateException("Error inflating font XML", e);
            }
            finally
            {
                if ( parser != null )
                    parser.close();
            }
        }
    
        public Typeface get(String family, int style)
        {
            for ( Font font : mFonts )
            {
                for ( String familyName : font.families )
                {
                    if ( familyName.equals(family) )
                    {
                        // if no style in specified, return normal style.
                        if ( style == -1 )
                            style = Typeface.NORMAL;
    
                        for ( FontStyle fontStyle : font.styles )
                        {
                            if ( fontStyle.style == style )
                                return fontStyle.font;
                        }
                    }
                }
            }
    
            return null;
        }
    }
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class FontableTextView extends TextView
    {
        private static final String TAG = "FontableTextView";
    
        public FontableTextView(Context context)
        {
            super(context);
        }
    
        public FontableTextView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            setCustomFont(context, attrs);
        }
    
        public FontableTextView(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
            setCustomFont(context, attrs);
        }
    
        /* 
         * @see android.widget.CompoundButton#onDraw(android.graphics.Canvas)
         */
        @Override
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
        }
    
        private void setCustomFont(Context ctx, AttributeSet attrs)
        {
            // Fonts work as a combination of particular family and the style. 
            TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.Fonts);
            String family = a.getString(R.styleable.Fonts_font);
            int style = a.getInt(R.styleable.Fonts_android_textStyle, -1);
            a.recycle();
            // Set the typeface based on the family and the style combination.
            if ( family != null )
            {
                setTypeface(FontManager.getInstance().get(family, style));
            }
        }
    }
    
    创建自定义视图ExFontableTextView.java

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    
    import android.content.Context;
    import android.content.res.XmlResourceParser;
    import android.graphics.Typeface;
    import android.view.InflateException;
    
    public class FontManager
    {
        //Making FontManager a singleton class
        private static class InstanceHolder
        {
            private static final FontManager INSTANCE = new FontManager();
        }
    
        public static FontManager getInstance()
        {
            return FontManager.InstanceHolder.INSTANCE;
        }
    
        private FontManager()
        {
        }
    
        // Different tags used in XML file.
        private static final String TAG_FAMILY = "family";
        private static final String TAG_NAMESET = "nameset";
        private static final String TAG_NAME = "name";
        private static final String TAG_FILESET = "fileset";
        private static final String TAG_FILE = "file";
    
        // Different styles supported.
        private static final String STYLE_BOLD = "-Bold.ttf";
        private static final String STYLE_ITALIC = "-Italic.ttf";
        private static final String STYLE_BOLDITALIC = "-BoldItalic.ttf";
    
        private class FontStyle
        {
            int style;
            Typeface font;
        }
    
        private class Font
        {
            // different font-family names that this Font will respond to.
            List<String> families;
    
            // different styles for this font.
            List<FontStyle> styles;
        }
    
        private List<Font> mFonts;
    
        //private boolean isFamilySet = false;
        private boolean isName = false;
        private boolean isFile = false;
    
        // Parse the resId and initialize the parser.
        public void initialize(Context context, int resId)
        {
            XmlResourceParser parser = null;
            try
            {
                parser = context.getResources().getXml(resId);
                mFonts = new ArrayList<Font>();
    
                String tag;
                int eventType = parser.getEventType();
    
                Font font = null;
    
                do
                {
                    tag = parser.getName();
    
                    switch ( eventType )
                    {
                        case XmlPullParser.START_TAG:
                            if ( tag.equals(TAG_FAMILY) )
                            {
                                // one of the font-families.
                                font = new Font();
                            }
                            else if ( tag.equals(TAG_NAMESET) )
                            {
                                // a list of font-family names supported.
                                font.families = new ArrayList<String>();
                            }
                            else if ( tag.equals(TAG_NAME) )
                            {
                                isName = true;
                            }
                            else if ( tag.equals(TAG_FILESET) )
                            {
                                // a list of files specifying the different styles.
                                font.styles = new ArrayList<FontStyle>();
                            }
                            else if ( tag.equals(TAG_FILE) )
                            {
                                isFile = true;
                            }
                            break;
    
                        case XmlPullParser.END_TAG:
                            if ( tag.equals(TAG_FAMILY) )
                            {
                                // add it to the list.
                                if ( font != null )
                                {
                                    mFonts.add(font);
                                    font = null;
                                }
                            }
                            else if ( tag.equals(TAG_NAME) )
                            {
                                isName = false;
                            }
                            else if ( tag.equals(TAG_FILE) )
                            {
                                isFile = false;
                            }
                            break;
    
                        case XmlPullParser.TEXT:
                            String text = parser.getText();
                            if ( isName )
                            {
                                // value is a name, add it to list of family-names.
                                if ( font.families != null )
                                    font.families.add(text);
                            }
                            else if ( isFile )
                            {
                                // value is a file, add it to the proper kind.
                                FontStyle fontStyle = new FontStyle();
                                fontStyle.font = Typeface.createFromAsset(context.getAssets(), text);
    
                                if ( text.endsWith(STYLE_BOLD) )
                                    fontStyle.style = Typeface.BOLD;
                                else if ( text.endsWith(STYLE_ITALIC) )
                                    fontStyle.style = Typeface.ITALIC;
                                else if ( text.endsWith(STYLE_BOLDITALIC) )
                                    fontStyle.style = Typeface.BOLD_ITALIC;
                                else
                                    fontStyle.style = Typeface.NORMAL;
    
                                font.styles.add(fontStyle);
                            }
                    }
    
                    eventType = parser.next();
    
                }
                while ( eventType != XmlPullParser.END_DOCUMENT );
    
            }
            catch ( XmlPullParserException e )
            {
                throw new InflateException("Error inflating font XML", e);
            }
            catch ( IOException e )
            {
                throw new InflateException("Error inflating font XML", e);
            }
            finally
            {
                if ( parser != null )
                    parser.close();
            }
        }
    
        public Typeface get(String family, int style)
        {
            for ( Font font : mFonts )
            {
                for ( String familyName : font.families )
                {
                    if ( familyName.equals(family) )
                    {
                        // if no style in specified, return normal style.
                        if ( style == -1 )
                            style = Typeface.NORMAL;
    
                        for ( FontStyle fontStyle : font.styles )
                        {
                            if ( fontStyle.style == style )
                                return fontStyle.font;
                        }
                    }
                }
            }
    
            return null;
        }
    }
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class FontableTextView extends TextView
    {
        private static final String TAG = "FontableTextView";
    
        public FontableTextView(Context context)
        {
            super(context);
        }
    
        public FontableTextView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            setCustomFont(context, attrs);
        }
    
        public FontableTextView(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
            setCustomFont(context, attrs);
        }
    
        /* 
         * @see android.widget.CompoundButton#onDraw(android.graphics.Canvas)
         */
        @Override
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
        }
    
        private void setCustomFont(Context ctx, AttributeSet attrs)
        {
            // Fonts work as a combination of particular family and the style. 
            TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.Fonts);
            String family = a.getString(R.styleable.Fonts_font);
            int style = a.getInt(R.styleable.Fonts_android_textStyle, -1);
            a.recycle();
            // Set the typeface based on the family and the style combination.
            if ( family != null )
            {
                setTypeface(FontManager.getInstance().get(family, style));
            }
        }
    }
    
    启动FontManager,在Main活动开始时设置自定义字体

    FontManager.getInstance().initialize(getApplicationContext(), R.xml.fonts);
    
    在布局XML中使用自定义视图

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <com.package.FontableTextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dip"
            android:text="FontableTextView"
            android:textStyle="bold"
            custom:font="Arial" />
    </RelativeLayout>
    

    使用如下自定义字体创建视图

    @Override
    public void setTitleTypeface(Typeface TF) {}
    @Override
    public void setSubtitleTypeface(Typeface TF) {}
    
    FontManager.java

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    
    import android.content.Context;
    import android.content.res.XmlResourceParser;
    import android.graphics.Typeface;
    import android.view.InflateException;
    
    public class FontManager
    {
        //Making FontManager a singleton class
        private static class InstanceHolder
        {
            private static final FontManager INSTANCE = new FontManager();
        }
    
        public static FontManager getInstance()
        {
            return FontManager.InstanceHolder.INSTANCE;
        }
    
        private FontManager()
        {
        }
    
        // Different tags used in XML file.
        private static final String TAG_FAMILY = "family";
        private static final String TAG_NAMESET = "nameset";
        private static final String TAG_NAME = "name";
        private static final String TAG_FILESET = "fileset";
        private static final String TAG_FILE = "file";
    
        // Different styles supported.
        private static final String STYLE_BOLD = "-Bold.ttf";
        private static final String STYLE_ITALIC = "-Italic.ttf";
        private static final String STYLE_BOLDITALIC = "-BoldItalic.ttf";
    
        private class FontStyle
        {
            int style;
            Typeface font;
        }
    
        private class Font
        {
            // different font-family names that this Font will respond to.
            List<String> families;
    
            // different styles for this font.
            List<FontStyle> styles;
        }
    
        private List<Font> mFonts;
    
        //private boolean isFamilySet = false;
        private boolean isName = false;
        private boolean isFile = false;
    
        // Parse the resId and initialize the parser.
        public void initialize(Context context, int resId)
        {
            XmlResourceParser parser = null;
            try
            {
                parser = context.getResources().getXml(resId);
                mFonts = new ArrayList<Font>();
    
                String tag;
                int eventType = parser.getEventType();
    
                Font font = null;
    
                do
                {
                    tag = parser.getName();
    
                    switch ( eventType )
                    {
                        case XmlPullParser.START_TAG:
                            if ( tag.equals(TAG_FAMILY) )
                            {
                                // one of the font-families.
                                font = new Font();
                            }
                            else if ( tag.equals(TAG_NAMESET) )
                            {
                                // a list of font-family names supported.
                                font.families = new ArrayList<String>();
                            }
                            else if ( tag.equals(TAG_NAME) )
                            {
                                isName = true;
                            }
                            else if ( tag.equals(TAG_FILESET) )
                            {
                                // a list of files specifying the different styles.
                                font.styles = new ArrayList<FontStyle>();
                            }
                            else if ( tag.equals(TAG_FILE) )
                            {
                                isFile = true;
                            }
                            break;
    
                        case XmlPullParser.END_TAG:
                            if ( tag.equals(TAG_FAMILY) )
                            {
                                // add it to the list.
                                if ( font != null )
                                {
                                    mFonts.add(font);
                                    font = null;
                                }
                            }
                            else if ( tag.equals(TAG_NAME) )
                            {
                                isName = false;
                            }
                            else if ( tag.equals(TAG_FILE) )
                            {
                                isFile = false;
                            }
                            break;
    
                        case XmlPullParser.TEXT:
                            String text = parser.getText();
                            if ( isName )
                            {
                                // value is a name, add it to list of family-names.
                                if ( font.families != null )
                                    font.families.add(text);
                            }
                            else if ( isFile )
                            {
                                // value is a file, add it to the proper kind.
                                FontStyle fontStyle = new FontStyle();
                                fontStyle.font = Typeface.createFromAsset(context.getAssets(), text);
    
                                if ( text.endsWith(STYLE_BOLD) )
                                    fontStyle.style = Typeface.BOLD;
                                else if ( text.endsWith(STYLE_ITALIC) )
                                    fontStyle.style = Typeface.ITALIC;
                                else if ( text.endsWith(STYLE_BOLDITALIC) )
                                    fontStyle.style = Typeface.BOLD_ITALIC;
                                else
                                    fontStyle.style = Typeface.NORMAL;
    
                                font.styles.add(fontStyle);
                            }
                    }
    
                    eventType = parser.next();
    
                }
                while ( eventType != XmlPullParser.END_DOCUMENT );
    
            }
            catch ( XmlPullParserException e )
            {
                throw new InflateException("Error inflating font XML", e);
            }
            catch ( IOException e )
            {
                throw new InflateException("Error inflating font XML", e);
            }
            finally
            {
                if ( parser != null )
                    parser.close();
            }
        }
    
        public Typeface get(String family, int style)
        {
            for ( Font font : mFonts )
            {
                for ( String familyName : font.families )
                {
                    if ( familyName.equals(family) )
                    {
                        // if no style in specified, return normal style.
                        if ( style == -1 )
                            style = Typeface.NORMAL;
    
                        for ( FontStyle fontStyle : font.styles )
                        {
                            if ( fontStyle.style == style )
                                return fontStyle.font;
                        }
                    }
                }
            }
    
            return null;
        }
    }
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class FontableTextView extends TextView
    {
        private static final String TAG = "FontableTextView";
    
        public FontableTextView(Context context)
        {
            super(context);
        }
    
        public FontableTextView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            setCustomFont(context, attrs);
        }
    
        public FontableTextView(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
            setCustomFont(context, attrs);
        }
    
        /* 
         * @see android.widget.CompoundButton#onDraw(android.graphics.Canvas)
         */
        @Override
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
        }
    
        private void setCustomFont(Context ctx, AttributeSet attrs)
        {
            // Fonts work as a combination of particular family and the style. 
            TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.Fonts);
            String family = a.getString(R.styleable.Fonts_font);
            int style = a.getInt(R.styleable.Fonts_android_textStyle, -1);
            a.recycle();
            // Set the typeface based on the family and the style combination.
            if ( family != null )
            {
                setTypeface(FontManager.getInstance().get(family, style));
            }
        }
    }
    
    创建自定义视图ExFontableTextView.java

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    
    import android.content.Context;
    import android.content.res.XmlResourceParser;
    import android.graphics.Typeface;
    import android.view.InflateException;
    
    public class FontManager
    {
        //Making FontManager a singleton class
        private static class InstanceHolder
        {
            private static final FontManager INSTANCE = new FontManager();
        }
    
        public static FontManager getInstance()
        {
            return FontManager.InstanceHolder.INSTANCE;
        }
    
        private FontManager()
        {
        }
    
        // Different tags used in XML file.
        private static final String TAG_FAMILY = "family";
        private static final String TAG_NAMESET = "nameset";
        private static final String TAG_NAME = "name";
        private static final String TAG_FILESET = "fileset";
        private static final String TAG_FILE = "file";
    
        // Different styles supported.
        private static final String STYLE_BOLD = "-Bold.ttf";
        private static final String STYLE_ITALIC = "-Italic.ttf";
        private static final String STYLE_BOLDITALIC = "-BoldItalic.ttf";
    
        private class FontStyle
        {
            int style;
            Typeface font;
        }
    
        private class Font
        {
            // different font-family names that this Font will respond to.
            List<String> families;
    
            // different styles for this font.
            List<FontStyle> styles;
        }
    
        private List<Font> mFonts;
    
        //private boolean isFamilySet = false;
        private boolean isName = false;
        private boolean isFile = false;
    
        // Parse the resId and initialize the parser.
        public void initialize(Context context, int resId)
        {
            XmlResourceParser parser = null;
            try
            {
                parser = context.getResources().getXml(resId);
                mFonts = new ArrayList<Font>();
    
                String tag;
                int eventType = parser.getEventType();
    
                Font font = null;
    
                do
                {
                    tag = parser.getName();
    
                    switch ( eventType )
                    {
                        case XmlPullParser.START_TAG:
                            if ( tag.equals(TAG_FAMILY) )
                            {
                                // one of the font-families.
                                font = new Font();
                            }
                            else if ( tag.equals(TAG_NAMESET) )
                            {
                                // a list of font-family names supported.
                                font.families = new ArrayList<String>();
                            }
                            else if ( tag.equals(TAG_NAME) )
                            {
                                isName = true;
                            }
                            else if ( tag.equals(TAG_FILESET) )
                            {
                                // a list of files specifying the different styles.
                                font.styles = new ArrayList<FontStyle>();
                            }
                            else if ( tag.equals(TAG_FILE) )
                            {
                                isFile = true;
                            }
                            break;
    
                        case XmlPullParser.END_TAG:
                            if ( tag.equals(TAG_FAMILY) )
                            {
                                // add it to the list.
                                if ( font != null )
                                {
                                    mFonts.add(font);
                                    font = null;
                                }
                            }
                            else if ( tag.equals(TAG_NAME) )
                            {
                                isName = false;
                            }
                            else if ( tag.equals(TAG_FILE) )
                            {
                                isFile = false;
                            }
                            break;
    
                        case XmlPullParser.TEXT:
                            String text = parser.getText();
                            if ( isName )
                            {
                                // value is a name, add it to list of family-names.
                                if ( font.families != null )
                                    font.families.add(text);
                            }
                            else if ( isFile )
                            {
                                // value is a file, add it to the proper kind.
                                FontStyle fontStyle = new FontStyle();
                                fontStyle.font = Typeface.createFromAsset(context.getAssets(), text);
    
                                if ( text.endsWith(STYLE_BOLD) )
                                    fontStyle.style = Typeface.BOLD;
                                else if ( text.endsWith(STYLE_ITALIC) )
                                    fontStyle.style = Typeface.ITALIC;
                                else if ( text.endsWith(STYLE_BOLDITALIC) )
                                    fontStyle.style = Typeface.BOLD_ITALIC;
                                else
                                    fontStyle.style = Typeface.NORMAL;
    
                                font.styles.add(fontStyle);
                            }
                    }
    
                    eventType = parser.next();
    
                }
                while ( eventType != XmlPullParser.END_DOCUMENT );
    
            }
            catch ( XmlPullParserException e )
            {
                throw new InflateException("Error inflating font XML", e);
            }
            catch ( IOException e )
            {
                throw new InflateException("Error inflating font XML", e);
            }
            finally
            {
                if ( parser != null )
                    parser.close();
            }
        }
    
        public Typeface get(String family, int style)
        {
            for ( Font font : mFonts )
            {
                for ( String familyName : font.families )
                {
                    if ( familyName.equals(family) )
                    {
                        // if no style in specified, return normal style.
                        if ( style == -1 )
                            style = Typeface.NORMAL;
    
                        for ( FontStyle fontStyle : font.styles )
                        {
                            if ( fontStyle.style == style )
                                return fontStyle.font;
                        }
                    }
                }
            }
    
            return null;
        }
    }
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class FontableTextView extends TextView
    {
        private static final String TAG = "FontableTextView";
    
        public FontableTextView(Context context)
        {
            super(context);
        }
    
        public FontableTextView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            setCustomFont(context, attrs);
        }
    
        public FontableTextView(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
            setCustomFont(context, attrs);
        }
    
        /* 
         * @see android.widget.CompoundButton#onDraw(android.graphics.Canvas)
         */
        @Override
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
        }
    
        private void setCustomFont(Context ctx, AttributeSet attrs)
        {
            // Fonts work as a combination of particular family and the style. 
            TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.Fonts);
            String family = a.getString(R.styleable.Fonts_font);
            int style = a.getInt(R.styleable.Fonts_android_textStyle, -1);
            a.recycle();
            // Set the typeface based on the family and the style combination.
            if ( family != null )
            {
                setTypeface(FontManager.getInstance().get(family, style));
            }
        }
    }
    
    启动FontManager,在Main活动开始时设置自定义字体

    FontManager.getInstance().initialize(getApplicationContext(), R.xml.fonts);
    
    在布局XML中使用自定义视图

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <com.package.FontableTextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dip"
            android:text="FontableTextView"
            android:textStyle="bold"
            custom:font="Arial" />
    </RelativeLayout>
    
    
    
  • 对于actionbar:
  • 使用actionbar sherlock()并自定义其代码:

    在com.actionbarsherlock.app.Actionbar中添加以下两种抽象方法:

    public abstract void setTitleTypeface(Typeface TF);
    public abstract void setSubtitleTypeface(Typeface TF);
    
    并在com.actionbarsherlock.internal.app.ActionBarImpl中重写这些方法

    @Override
    public void setTitleTypeface(Typeface TF) {
        mActionView.setTitleTypeface(TF);     }
    
    @Override
    public void setSubtitleTypeface(Typeface TF) {
        mActionView.setSubtitleTypeface(TF);    }
    
    在com.actionbarsherlock.internal.app.ActionBarWrapper中也是这样

    @Override
    public void setTitleTypeface(Typeface TF) {}
    @Override
    public void setSubtitleTypeface(Typeface TF) {}
    
    最后,在com.actionbarsherlock.internal.widget.ActionBarView中添加以下方法:

     public void setTitleTypeface(Typeface TF){
        mTitleView.setTypeface(TF);
    }
    
    public void setSubtitleTypeface(Typeface TF){
        mSubtitleView.setTypeface(TF);
    }
    
    现在在你的夏洛克活动中使用它,如下所示:

        Typeface TF = Typeface.createFromAsset(getApplication().getAssets(),
                    "Arial.ttf");
        getSupportActionBar().setTitleTypeface(TF);
    
    确保没有更好的方法

    2。对于对话框、系统设置,。。。你应该像这样修改代码

  • 对于actionbar:
  • 使用actionbar sherlock()并自定义其代码:

    在com.actionbarsherlock.app.Actionbar中添加以下两种抽象方法:

    public abstract void setTitleTypeface(Typeface TF);
    public abstract void setSubtitleTypeface(Typeface TF);
    
    并在com.actionbarsherlock.internal.app.ActionBarImpl中重写这些方法

    @Override
    public void setTitleTypeface(Typeface TF) {
        mActionView.setTitleTypeface(TF);     }
    
    @Override
    public void setSubtitleTypeface(Typeface TF) {
        mActionView.setSubtitleTypeface(TF);    }
    
    在com.actionbarsherlock.internal.app.ActionBarWrapper中也是这样

    @Override
    public void setTitleTypeface(Typeface TF) {}
    @Override
    public void setSubtitleTypeface(Typeface TF) {}
    
    最后,在com.actionbarsherlock.internal.widget.ActionBarView中添加以下方法:

     public void setTitleTypeface(Typeface TF){
        mTitleView.setTypeface(TF);
    }
    
    public void setSubtitleTypeface(Typeface TF){
        mSubtitleView.setTypeface(TF);
    }
    
    现在在你的夏洛克活动中使用它,如下所示:

        Typeface TF = Typeface.createFromAsset(getApplication().getAssets(),
                    "Arial.ttf");
        getSupportActionBar().setTitleTypeface(TF);
    
    确保没有更好的方法


    2。对于对话框、系统设置,。。。你应该像这样修改代码

    我认为你应该使用风格,在这里阅读主题你能给我举个例子吗?我没有找到任何方法来改变自定义字体的风格-只有编程我认为你应该使用风格,主题阅读这里你能给我一个例子吗?我没有找到任何方法来改变自定义字体的风格-只有编程的ActionBarSherlock不是一个系统组件。我没有说那是一个系统组件!问题是关于系统组件。但它是ActionBar系统组件的替代品!actionbarsherlock不是一个系统组件。我没有说那是一个系统组件!问题是关于系统组件。但它是ActionBar系统组件的替代品!