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

时间:2018-10-18 01:31:31

标签: android colors keyboard popup android-softkeyboard

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

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

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

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

@Graeme提到

  

如果要更改弹出窗口的布局/样式(默认为@android:layout / keyboard_popup_keyboard.xml),则可以指定android:popupLayout属性,该属性指向布局文件

因此,我制作了自己的版本的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商店免费提供。有用。但是,人们由于令人敬畏的色彩设计而犹豫不决。一旦弹出式窗口变色,我将发布一个新版本。谢谢。

1 个答案:

答案 0 :(得分:0)

由于两个月没有答案了,所以我花了一些时间进行更多的拨弄和猜测。现在,我今天很幸运,希望对下一位语言学家感到高兴,他也需要自定义键盘,并且需要通过示例进行工作:

mykeyboard.java 指向主键盘的布局文件,因此(第三行“输入”)。我只给出三行引用:

    @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-defintions:

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

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

enter image description here

相关问题