问题描述
我正在 Android 上实现自定义键盘.我刚刚阅读了有关developer.android.com"的文档并看到了带有软键盘的示例.我所能做的就是改变键盘背景,改变按钮的位置,将 keyIcon 而不是 keyLabel 设置为 key.
I am implementing custom keyboard on Android. I have just read documentation on "developer.android.com" and seen sample with soft keyboard. All I can - it`s to change keyboard background, change placement of buttons, set keyIcon instead of keyLabel to key.
但是我还是不能改变key的背景和颜色.
But I still can not change key`s background and color.
请写一些 XML 或源代码的示例代码.谢谢!
Please write some sample code of XML or source. Thanks!
我更改背景的示例:
public class GBInput extends InputMethodService implements KeyboardView.OnKeyboardActionListener{ ... private GBKeyboardView mInputView; @Override public View onCreateInputView() { mInputView = (GBKeyboardView) getLayoutInflater().inflate(R.layout.input, null); mInputView.setOnKeyboardActionListener(this); mInputView.setKeyboard(mQwertyKeyboard); mInputView.setBackgroundResource(R.color.keyboard_background); return mInputView; } ... }
我需要这样的东西:
所有按钮的图像 - 这是个坏主意,所以我想找到更好的问题.
Images for all buttons - its bad idea, so I want to find better issue.
推荐答案
将这行代码添加到你的 input.xml 中
Add this line of code to your input.xml
android:keyBackground="@drawable/samplekeybackground"
所以您的 input.xml 最终应该看起来与此类似.
So your input.xml should end up looking similar to this.
<com.example.keyboard.LatinKeyboardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/keyboard" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:keyPreviewLayout="@layout/input_key_preview" android:keyBackground="@drawable/samplekeybackground" />
如果您已经有一个可以使用的drawable,否则这里有一个示例关键背景drawable,它将产生与您的示例图像类似的结果.
If you already have a drawable to use great otherwise here is a sample key background drawable that will produce results like your example image.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/normal" /> <!-- Pressed state --> <item android:state_pressed="true" android:drawable="@drawable/pressed" /></selector>
如果您想要 xml 中的所有内容,您可以使用 shape xml,如下所示.可绘制/normal.xml
If you want everything in xml you can use shape xml's like the following. drawable/normal.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="4dp" android:color="#000000" /> <solid android:color="#FFFFFF"/> </shape>
和drawable/pressed.xml
and drawable/pressed.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="4dp" android:color="#A3D9F3" /> <solid android:color="#4ABCE8"/> </shape>