Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.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
Java 在运行时添加字体资源_Java_Android_Fonts_Textview_Runtime - Fatal编程技术网

Java 在运行时添加字体资源

Java 在运行时添加字体资源,java,android,fonts,textview,runtime,Java,Android,Fonts,Textview,Runtime,我正在尝试向我的应用程序动态添加字体。我想在运行时根据我的服务器指示的字体更改我的应用程序的每个文本视图的字体。有没有办法下载字体(ttf文件或其他)并在运行时使用 提前谢谢。超级酷的问题,所以我将尝试一下,并为您指出正确的方向,因为我肯定认为这是可能的 首先,我想到了几件事: 下载字体的东西 用来保存激活字体的东西 当字体不可用时处理它的一些方法(或者,更简单的是,在字体可用之前,不要显示任何内容) 使用活动字体的自定义文本视图 我将把#1留给你,因为我相信下载部分有点超出了如何实际使用字体的

我正在尝试向我的应用程序动态添加字体。我想在运行时根据我的服务器指示的字体更改我的应用程序的每个文本视图的字体。有没有办法下载字体(ttf文件或其他)并在运行时使用


提前谢谢。

超级酷的问题,所以我将尝试一下,并为您指出正确的方向,因为我肯定认为这是可能的

首先,我想到了几件事:

  • 下载字体的东西
  • 用来保存激活字体的东西
  • 当字体不可用时处理它的一些方法(或者,更简单的是,在字体可用之前,不要显示任何内容)
  • 使用活动字体的自定义文本视图
  • 我将把#1留给你,因为我相信下载部分有点超出了如何实际使用字体的范围,下载文件的方法很多

    对于#2,我们可以使用单例来保存对活动字体的引用(这样我们就不会为每个想要使用它的视图重新创建它):

    如您所见,使用它,我们可以轻松地更改实例中的活动字体,并将对文件的引用存储在首选项中,以便在会话之后保持。如果要添加不同的变体(例如粗体、斜体等),可以修改模板

    它还有一个对资源文件的引用,当当前未保存任何字体时,该文件将默认使用该字体

    现在我们需要一个自定义文本视图来使用这种字体:

    public class DynamicFontTextView extends TextView {
    
        public DynamicFontTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            updateActiveFont();
        }
    
        public DynamicFontTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            updateActiveFont();
        }
    
        public DynamicFontTextView(Context context) {
            super(context);
            updateActiveFont();
        }
    
        @Override
        public void setTypeface(Typeface tf, int style) {
            // if(style == Typeface.BOLD) <-- Something for later
            super.setTypeface(FontHolder.getInstance().getActiveFont());
        }
    
        public void updateActiveFont(){
            super.setTypeface(FontHolder.getInstance().getActiveFont());
        }
    }
    
    在这种情况下,让我们使用A,因为它相对简单:只需创建一个启动页面,在字体下载之前不允许用户继续

    同样,初始页面有点超出了本文的范围,但希望它能为您指明如何完成任务的正确方向

    public class DynamicFontTextView extends TextView {
    
        public DynamicFontTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            updateActiveFont();
        }
    
        public DynamicFontTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            updateActiveFont();
        }
    
        public DynamicFontTextView(Context context) {
            super(context);
            updateActiveFont();
        }
    
        @Override
        public void setTypeface(Typeface tf, int style) {
            // if(style == Typeface.BOLD) <-- Something for later
            super.setTypeface(FontHolder.getInstance().getActiveFont());
        }
    
        public void updateActiveFont(){
            super.setTypeface(FontHolder.getInstance().getActiveFont());
        }
    }
    
    <com.package.DynamicFontTextView
     ....
     />
    
    A. Prevent them from getting to a screen where the custom font would ever be used.
    B. Render with a default font, and then update the Views once the font is available