【问题标题】:How to set a different background for function key in xml on keyboard?如何为键盘上的xml中的功能键设置不同的背景?
【发布时间】:2012-07-17 12:36:12
【问题描述】:

我正在为 Android 开发一个键盘应用程序。我尝试为普通键和功能键设置不同的背景,但没有成功:

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

【问题讨论】:

    标签: android xml function keyboard key


    【解决方案1】:

    有一个名为 android:keyBackground 的 XML 属性。只需将此属性设置为可绘制对象就可以了。

    将此属性添加到 input.xml 中的 KeyboardView:

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

    我假设这是您用来为所有键分配背景颜色的方法。

    对于单独的功能键,创建一个适当的前景可绘制图像以覆盖整个键,并将其分配给键盘布局 xml 的 xml 中的keyIcon

    为了完美,请使用覆盖相同尺寸的背景图片,以防您不想更改 keyIcon

    【讨论】:

    • 感谢您的回答是的,我使用 android:keyBackground 来设置选择器。问题是当我设置 keyIcon 时,它并没有涵盖所有屏幕尺寸的所有键。
    • 每个按键都有背景图,也可以设置
    • android:keyIcon 是您最好的选择!关于屏幕尺寸,我相信它可以通过编程方式完成,或者您可以为可绘制资源使用限定符
    【解决方案2】:

    这个 hack 对我有用。

    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()中可以根据需要编写更多条件

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 2019-03-22
      • 2013-03-25
      相关资源
      最近更新 更多