【问题标题】:Problem with Custom Keyboard not applying color change自定义键盘不应用颜色更改的问题
【发布时间】:2019-02-11 22:44:49
【问题描述】:

我已关注this tutorial 并创建了自己的键盘,但我无法更改其按键的背景颜色。

keyboard.xml

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="3px"
android:keyWidth="10%p"
android:keyHeight="60dp"
android:keyBackground="@color/color_green" 
android:keyTextColor="@color/color_red"
android:verticalGap="3px">

keyboard_view.xml

<android.inputmethodservice.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/all_white">
</android.inputmethodservice.KeyboardView>

MyInputMethodService.java

    @Override
public View onCreateInputView() {
    keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard_view, null);
    keyboardView.setPreviewEnabled(false);
    keyboard = new Keyboard(this, R.xml.number_pad);
    keyboardView.setKeyboard(keyboard);
    keyboardView.setOnKeyboardActionListener(this);
    return keyboardView;
}

我尝试更改 android:keyBackgroundandroid:keyTextColor,但没有任何变化,但如果我更改同一文件中的某些键值,它会起作用。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我知道这是一个老问题,KeyboardView 现在已被弃用(尽管我正在玩弄它),但对于将来出于任何原因遇到此问题的任何人,这里有一个解决方案:

    keyTextColorkeyBackground 设置为KeyboardView,而不是Keyboard

    另外,keyBackground 必须是图像,而不是十六进制颜色代码。

    KeyboardView 更改为以下内容:

    <?xml version="1.0" encoding="utf-8"?>
    <android.inputmethodservice.KeyboardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/keyboard_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:keyPreviewLayout="@layout/key_preview"
        android:layout_alignParentBottom="true"
        android:keyTextColor="#FF0000"
        android:keyBackground="@drawable/ic_launcher_background">
    </android.inputmethodservice.KeyboardView>
    

    你会得到类似的东西:

    【讨论】:

      猜你喜欢
      • 2017-11-04
      • 2016-12-03
      • 2017-02-14
      • 2019-08-25
      • 2014-05-02
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多