【问题标题】:How to set SquareTextView TypeFace如何设置 SquareTextView 字体
【发布时间】:2016-03-27 14:13:57
【问题描述】:

我正在尝试使用SquareTextView 为地图标记的群集图标设置自定义字体,android-maps-utils-amap 库扩展 TextView 的类是什么。在DefaultClusterRenderer 中,我使用此代码设置自定义字体但没有效果。所以请帮助我了解我需要做什么来更改 SquareTextView 的字体

private SquareTextView makeSquareTextView(Context context) {
    SquareTextView squareTextView = new SquareTextView(context);            
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "whatever.ttf");
    squareTextView.setTypeface(typeface);
}

这是SquareTextView的源代码:

package com.amap.api.maps2d.ui;

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

public class SquareTextView extends TextView {
    private int mOffsetTop = 0;
    private int mOffsetLeft = 0;

    public SquareTextView(Context context) {
        super(context);
    }

    public SquareTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        int dimension = Math.max(width, height);
        if (width > height) {
            mOffsetTop = width - height;
            mOffsetLeft = 0;
        } else {
            mOffsetTop = 0;
            mOffsetLeft = height - width;
        }
        setMeasuredDimension(dimension, dimension);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.translate(mOffsetLeft / 2, mOffsetTop / 2);
        super.draw(canvas);
    }
}

【问题讨论】:

  • 您确定在资产目录中有Roboto-Italic.ttf 文件,而不是在任何子目录中吗?另外,如果你想设置应用程序宽字体,你可以尝试使用Calligraphy 并跳过所有这些 setTypeface 废话

标签: android textview android-fonts android-typeface android-maps-utils


【解决方案1】:

我有自己的 TextView 实现,我用它来设置如下字体,它对我有用:

public class MyNewTextView extends TextView {
private int mOffsetTop = 0;
private int mOffsetLeft = 0;

public MyNewTextView(Context context) {
    super(context);
}

public MyNewTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public MyNewTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void setTypeface(Typeface tf, int style) {
     super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),   "fonts/whatever.ttf"), style);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    int dimension = Math.max(width, height);
    if (width > height) {
        mOffsetTop = width - height;
        mOffsetLeft = 0;
    } else {
        mOffsetTop = 0;
        mOffsetLeft = height - width;
    }
    setMeasuredDimension(dimension, dimension);
}

@Override
public void draw(Canvas canvas) {
    canvas.translate(mOffsetLeft / 2, mOffsetTop / 2);
    super.draw(canvas);
}
}

所以尝试为 SquareTextView 覆盖 setTypeface()

【讨论】:

  • 但是 SquareTextView 是一个库类,所以我不能编辑它,在我的情况下我不能使用 SquareTextView 以外的 TextView
  • 我在 SquareTextView 的实现中看到的是,您可以将 onMeasure() 和 onDraw() 实现复制到您的 OwnTextView,请检查我的编辑。好吗?
  • 谢谢,我正在使用它来更改集群 textview 的字体,所以我需要在 DefaultClusterRenderer 中使用它,其中不能使用扩展 TextView 的类,但即使我扩展 SquareTextView(就像在第二个答案)在任何情况下它都不再起作用
【解决方案2】:

自己实现SquareTextView试试这个:

public class MySquareTextView extends SquareTextView {

    public MySquareTextView (Context context) {
        super(context);
        setTypeface();
    }

    public MySquareTextView (Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeface();
    }

    public MySquareTextView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeface();
    }


    public void setTypeface(){
        setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Italic.ttf"));
    }
}

【讨论】:

  • 如果 squareTextView.setTypeface(typeface); 没有区别不工作这意味着 mySquareTextView.setTypeface(typeface);也行不通,因为实际上在这两种情况下它都使用相同的东西
  • 你试过了吗?我的应用程序中有相同的代码并且它正在工作。如果它不起作用,那么您的 font 路径是错误的。
  • 是的,我做了,没有结果和路径是好的,你是用它来改变标记的集群图标文本的字体吗?
  • 我用它把TypeFace改成TextView
  • 在简单的 TextView 中它也适用于我的情况,但使用集群图标则不适用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-07
  • 2014-09-05
  • 2012-05-22
相关资源
最近更新 更多