2012-07-18 101 views
6

我正在開發Android版鍵盤應用程序。我試圖爲正常鍵和功能鍵不同的背景,但它沒有工作:如何在鍵盤上爲xml中的功能鍵設置不同的背景?

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- Functional keys. --> 

    <item android:state_single="true" android:state_pressed="true" 
      android:drawable="@drawable/btn_keyboard_special" /> 
    <item android:state_single="true" 
      android:drawable="@drawable/btn_keyboard_special" /> 

    <!-- Toggle keys. Use checkable/checked state. --> 

    <item android:state_checkable="true" android:state_checked="true" android:state_pressed="true" 
      android:drawable="@drawable/btn_keyboard_key_dark_pressed_on" /> 
    <item android:state_checkable="true" android:state_pressed="true" 
      android:drawable="@drawable/btn_keyboard_key_dark_pressed_off" /> 
    <item android:state_checkable="true" android:state_checked="true" 
      android:drawable="@drawable/btn_keyboard_key_dark_normal_on" /> 
    <item android:state_checkable="true" 
      android:drawable="@drawable/btn_keyboard_key_dark_normal_off" /> 

    <!-- Normal keys --> 

    <item android:state_pressed="true" 
      android:drawable="@drawable/glow" /> 
    <item android:drawable="@drawable/btn_keyboard_key_light_normal" /> 
</selector> 

回答

2

有一個XML屬性名爲Android:keyBackground。只需將該屬性設置爲drawable即可,應該沒問題。

這attrbute添加到KeyboardView在input.xml中:

<KeyboardView android:keyBackground="@drawable/buttonbgselector" .../> 

我假設這是您正在使用指定的背景顏色爲所有的鍵是什麼。

僅針對功能鍵,創建一個合適的前景可繪製圖像來覆蓋整個按鍵,並將其分配到鍵盤佈局xml的xml中的keyIcon

是完美的,使用覆蓋相同尺寸也,櫃面你不想改變keyIcon

+0

感謝您的回答 是的,我使用Android:keyBackground設置選擇器。問題是當我設置keyIcon時,它並沒有覆蓋所有屏幕尺寸的所有鍵。 – Aydroid 2012-07-19 10:52:06

+0

每個鍵都有可以設置的背景圖片 – gaara87 2012-07-19 16:25:51

+0

我不知道背景圖片的屬性名稱 android:xxxx ???? – Aydroid 2012-07-19 17:52:52

2

這劈爲我工作的背景圖像。

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(); 
     paint.setTextAlign(Paint.Align.CENTER); 
     paint.setTextSize(25); 
     paint.setColor(Color.RED); 




     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; 
     } 
    } 

}

改變你的佈局XML來

<com.example.yourpackage.MyKeyboardView 
     android:id="@+id/keyboardview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:visibility="gone" 
     android:background="#000000" 
      android:keyBackground="@drawable/keyboard_selector" /> 

您可能的onDraw()寫多個條件按需要