3

I have used "Android Icon Set" (in eclipse), which has created different size icons in various drawable folders. Now when I am setting this icon using following code:

<Key android:codes="49" android:keyIcon="@drawable/my1" android:keyWidth="7%p"/>

Icon is not taking the full keysize, it is coming in center for some devices and coming outside the key in other devices. I need to fix this urgently please guide me. My main requirement is that I want certain different color keys on my custom keyboard (based on samplesoft keyboard), as it is difficult to change key background I am using images and now images are not occupying full space.

4

3 回答 3

3

我实际上只是自己遇到了这个问题。不幸的是,我得出的结论是,为了让它工作的努力,我最好忽略 KeyboardView 而只是使用按钮构建我自己的。KeyboardView 不提供任何非常有价值的东西,通过构建您自己的,您可以获得完全的控制权。

查看 android 内置键盘的源代码https://android.googlesource.com/platform/packages/inputmethods/LatinIME/我看到谷歌已经重新实现了他们自己的 KeyboardView,它支持不同的背景颜色以及许多其他功能。如果我的键盘需求很复杂,我会考虑从 LatinIME 项目中提取相关代码并重新使用它。

于 2013-08-14T12:46:37.770 回答
2

如果您厌倦了寻找解决方案。你必须像我一样尝试用你自己的逻辑覆盖你的 KeyboardView 的 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();
        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;
        }
    }
于 2014-01-14T01:51:41.950 回答
-2

你不能使用这样的颜色:

在文件夹drawable中,创建key_diff.xml,其中应包含:

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
      <solid android:color="#AABBCC"/> 
    </shape>

例如,为普通键 ( keys_norm.xml) 创建第二个形状:

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
      <solid android:color="#0000CC"/> 
    </shape>

在您的情况下,我认为困难的是控制drawabe的大小,以防它是图像(还考虑到“容器”具有固定的比例大小并且无法控制它的bg)。

于 2013-08-14T10:53:01.387 回答