如何更改android软键盘键的背景颜色?

我正在 Android上实现自定义键盘.我刚刚阅读了“developer.android.com”上的文档,并看到了使用软键盘的示例.我只能 – 改变键盘背景,改变按钮位置,将keyIcon而不是keyLabel设置为键.

但我仍然无法改变关键的背景和颜色.

请写一些XML或源代码示例代码.谢谢!

我改变背景的样本:

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

我需要这样的东西:

所有按钮的图像 – 它的坏主意,所以我想找到更好的问题.

解决方法

将此行代码添加到input.xml
android:keyBackground="@drawable/samplekeybackground"

所以你的input.xml应该看起来与此类似.

<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将产生像你的示例图像.

<?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中的所有内容,你可以使用如下形状xml.
绘制/ 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

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...