3

我有自定義MyKBV類擴展KeyBoardView.I創建此自定義視圖使用自定義字體的鍵。我能夠在鍵上看到更改的字體,但問題是每個鍵與XML中的默認鍵重疊,我認爲它是TypefaceE.DEFAULT_BOLD.SO我看到的是每個鍵上的兩個字符串,一個是粗體,另一個是字體我想要。我該如何去清除默認鍵,以便只有自定義的可見。我花了很多時間在這。如果有人能指出我要去哪裏錯了或我能做什麼do.Thanks!Android自定義KeyBoardView清除默認鍵

public class MyKBV extends KeyboardView { 
Context context; 

@Override 
public void onDraw(Canvas canvas) { 
    // TODO Auto-generated method stub 
    super.onDraw(canvas); 
    Paint paint = new Paint(); 
    Typeface font = Typeface.createFromAsset(context.getAssets(), 
      "fonts/Hippie.otf"); 
    paint.setTypeface(font); 
    paint.setTextSize(40); 

    List<Key> listKeys = getKeyboard().getKeys(); 

    for (Key key : listKeys) { 
     if (key.label != null) { 
      if (key.label.toString().length() > 1) { 
       paint.setTextSize(30); 
       canvas.drawText(key.label.toString(), key.x 
         + (key.width/2) - 15, key.y + (key.height/2) 
         + 10, paint); 
      } else { 
       canvas.drawText(key.label.toString(), key.x 
         + (key.width/2) - 10, key.y + (key.height/2) 
         + 10, paint); 
      } 
     } 
    } 

} 

public MyKeyBoardView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
    this.context = context; 

} 

}

+0

嗯,你有沒有試過不打電話super.onDraw?這將阻止底層鍵盤繪製自己..假設你想自己做所有的繪圖.. –

+0

你有沒有找到任何解決方案@luckysing – Fay007

回答

0

如果重寫的onDraw(),你應該先畫背景然後繪製你的文字。

public class MyKeyboardView extends android.inputmethodservice.KeyboardView { 

    Context context; 
    public MyKeyboardView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
     this.context = context ; 
    } 



    @Override 
    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     Paint paint = new Paint(); 
    Typeface font = Typeface.createFromAsset(context.getAssets(), 
      "fonts/Hippie.otf"); 
    paint.setTypeface(font); 
    paint.setTextSize(40); 




     List<Key> keys = getKeyboard().getKeys(); 
     for(Key key: keys) { 

    if(key.pressed){ 
       NinePatchDrawable npd = (NinePatchDrawable)context.getResources().getDrawable(R.drawable.glow); 
       npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height); 
       npd.draw(canvas); 
      if(key.label != null) 
       canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint); 
    }else if(key.modifier){ // boolean that defines key is function key 

      NinePatchDrawable npd = (NinePatchDrawable)context.getResources().getDrawable(R.drawable.btn_keyboard_special); 
      npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height); 
      npd.draw(canvas); 
      if(key.label != null) 
       canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint); 
     } 


     break; 
     } 
    } 
相關問題