0

我試圖使用SquareTextView來設置地圖標記的集羣圖標的自定義字體,什麼是android-maps-utils-amap庫擴展TextView的類。在DefaultClusterRenderer我使用此代碼來設置自定義字體,但沒有效果。所以,請幫助我明白我需要做什麼來改變SquareTextView如何設置SquareTextView TypeFace

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); 
    } 
} 
+0

你確定你的資產目錄'的Roboto-Italic.ttf'文件,而不是在任何子目錄?另外,如果你想設置應用程序寬字體 – Sourabh

回答

0

我有自己實施的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); 
} 
} 

所以要儘量覆蓋setTypeface()用於SquareTextView

+0

,你可以嘗試使用'Calligraphy'並跳過所有setTypeface廢話。但SquareTextView是一個庫類,所以我不能編輯它,在我的情況下,我不能使用TextView其他比SquareTextView – Kar4

+0

我在SquareTextView的實現中看到的是,您可以將onMeasure()和onDraw()實現複製到OwnTextView,請檢查我的編輯。可以嗎? – Amt87

+1

謝謝,我正在使用它來更改集羣textview的字體,所以我需要使用它在DefaultClusterRenderer中無法使用擴展TextView的類,但即使我擴展了SquareTextView(就像在第2個答案中) case it does not work again – Kar4

0

讓自己實現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")); 
    } 
} 
+0

如果squareTextView.setTypeface(字體)沒有區別;不工作,這意味着mySquareTextView.setTypeface(字體);也不會工作,因爲事實上在兩種情況下它都使用相同的東西 – Kar4

+0

你試過了嗎?我的應用程序中有相同的代碼,它正在工作。如果它不工作,那麼你的'font'路徑是錯誤的。 –

+0

是的,我做了,沒有結果和路徑是好的,你用它來改變標記的集羣圖標文本的字體? – Kar4