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

How to change background color of popup-keyboard for android soft keyboard?

我花了很长时间才弄清楚如何摆脱丑陋的黑色默认设置并为我的自定义键盘着色。

我从这个非常有用的答案开始工作,现在我可以很好地为我的键盘着色: How to change background color of key for android soft keyboard?

只是弹出键盘仍然是默认颜色。

我找到了另一个有用的答案,几乎让我找到了解决方案。但答案集中在弹出窗口的创建和预览上: Creating a SoftKeyboard with Multiple/Alternate characters per key

@Graeme 提到了

If you want to change the layout/style of the popup (which defaults to @android:layout/ keyboard_popup_keyboard.xml) you can specify a android:popupLayout attribute which points to a layout file

所以我制作了自己的 keyboard_popup_keyboard.xml 版本,并将其放在我的主布局文件 input.xml 旁边,放入 /res/layout 并引用它,就像在给定的示例中一样.

<org.gasana.android.aniikeyboard.LatinKeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:keyBackground="@drawable/samplekeybackground"
    android:keyTextColor="#000000"
    android:popupLayout="@layout/popup"
/>

遗憾的是,没有 popupLayout 文件的示例。所以我从

一路复制了原始文件
C:\Users\martin\AppData\Local\Android\sdk\platforms\android-28\data\res\layout\keyboard_popup_keyboard.xml

并尝试将其调整为 popup.xml 以使用与我的主键盘相同的背景:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/samplekeybackground"
    >
<android.inputmethodservice.KeyboardView
    android:id="@android:id/keyboardView"
    android:background="@drawable/samplekeybackground"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:popupLayout="@layout/popup"
    android:keyTextSize="22sp"
    tools:ignore="ResourceCycle" />

<ImageButton android:id="@android:id/closeButton"
    android:background="@drawable/samplekeybackground"
    android:src="@drawable/btn_close" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginStart="8dp"
    android:clickable="true"
    />

我的键盘仍然可以构建并创建一个可用的 APK。只是弹出窗口的颜色仍然是丑陋的默认值。

上下文:我是语言学家,不是开发人员。我为少数民族语言制作了这个带有特殊字母表和声调标记的自定义键盘,并在 Play 商店免费提供。有用。但是人们在犹豫,因为色彩设计太棒了。一旦我将弹出窗口着色,我将发布一个新版本。谢谢。

由于两个月没有答案,我花时间进行了更多的探索和猜测。现在我今天很幸运,想对下一位语言学家好一点,他也需要一个自定义键盘并且需要从示例中工作:

mykeyboard.java指向主键盘的布局文件所以(第三行"input")。我只给出三行引用:

    @Override public View onCreateInputView() {
    mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
            R.layout.input, null);

所以在我的 \res\layout\input.xml 中,我添加了对弹出布局的引用:

<org.my.project.here.LatinKeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:keyBackground="@drawable/samplekeybackground"
    android:keyTextColor="#000000"
    android:popupLayout="@layout/popup"  <!-- here it is -->
/>

我的\res\layout\popup.xml看起来像这样;我相信我是从提供的示例项目中复制的。今天我只是将两条标记线更改为浅蓝色背景颜色和黑色文本颜色,终于成功了。似乎我之前有循环引用但没有错误消息,只有丑陋的黑色默认布局。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/samplekeybackground">
<android.inputmethodservice.KeyboardView
    android:id="@android:id/keyboardView"
    android:background="@color/colorPrimary"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:keyTextSize="22sp"
    android:keyBackground="@drawable/samplekeybackground"  <!-- here it is -->
    android:keyTextColor="#000000"                         <!-- and here -->
    tools:ignore="ResourceCycle"/>
<ImageButton android:id="@android:id/closeButton"
    android:background="@android:color/transparent"
    android:src="@drawable/btn_close" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginStart="8dp"
    android:clickable="true"/>

提到的samplekeybackground.xml只是一个很简单的定义,指向两个实际的xml-colour-definitions:

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

为了完整起见,因为我喜欢可以复制和玩测试的东西,这里是 normal.xml; pressed.xml 是一样的,只是更深的蓝色:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="1dp" android:color="#A1B7F7" />
    <solid android:color="#C7D4FA"/>
</shape>

所有这一切都是从猜测和构建许多版本直到我幸运。可能无法回答任何后续问题,但它确实有效: