【发布时间】: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