简体   繁体   English

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

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

It took me a long time to figure out how to get rid of the ugly black default and color my custom keyboard. 我花了很长时间才弄清楚如何摆脱难看的黑色默认设置并为自定义键盘上色。

I worked from this very helpful answer and I can now color my keyboard nicely: How to change background color of key for android soft keyboard? 我从这个非常有帮助的答案开始工作,现在可以很好地键盘着色: 如何更改android软键盘按键的背景颜色?

Just the popup-keyboards are still in the default colors. 只是弹出键盘仍为默认颜色。

I found another helpful answer, which took me almost to a solution. 我找到了另一个有用的答案,这几乎使我找到了解决方案。 But the answer is focusing on the creation and preview of the popups: Creating a SoftKeyboard with Multiple/Alternate characters per key 但是答案是集中在弹出窗口的创建和预览上: 创建每个键具有多个/备用字符的软键盘

@Graeme has mentioned @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 如果要更改弹出窗口的布局/样式(默认为@android:layout / keyboard_popup_keyboard.xml),则可以指定android:popupLayout属性,该属性指向布局文件

So I have made my own version of keyboard_popup_keyboard.xml and put it next to my main layout file input.xml into /res/layout and made a reference to it, like in the example given. 因此,我制作了自己的版本的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"
/>

Sadly there was no example for the popupLayout file. 遗憾的是,没有关于popupLayout文件的示例。 So I copied the original file all the way up from 所以我从头开始一直复制原始文件

C:\Users\martin\AppData\Local\Android\sdk\platforms\android-28\data\res\layout\keyboard_popup_keyboard.xml

and tried to tweak it as popup.xml to use the same background as my main keyboard: 并尝试将其调整为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"
    />

My keyboard still builds and creates a working APK. 我的键盘仍在构建并创建有效的APK。 Just the color of the popups is still the ugly default. 只是弹出窗口的颜色仍然是丑陋的默认设置。

Context: I am a linguist, not a developper. 背景:我是一名语言学家,而不是开发人员。 I made this custom keyboard for a minority language with a special alphabet and tone-markers and have it free on the Play Store. 我为这种少数族裔语言制作了这种自定义键盘,并带有特殊的字母和音调标记,并在Play商店免费提供。 It works. 有用。 But people are hesitating, because of the aweful color-design. 但是,人们由于令人敬畏的色彩设计而犹豫不决。 As soon as I get the popups colored, I will publish a fresh version. 一旦弹出式窗口变色,我将发布一个新版本。 Thank you. 谢谢。

Since no answer was coming here for two months, I took time for more poking and guessing. 由于两个月来没有任何答案,我花时间进行更多的猜测和猜测。 Now I got lucky today and want to be nice to the next linguist, who also needs a custom keyboard and needs to work from examples: 现在,我今天很幸运,并希望与下一位语言学家融洽相处,后者也需要自定义键盘并且需要通过示例进行工作:

mykeyboard.java is pointing to the layout file for the main keyboard so (third line "input"). mykeyboard.java指向主键盘的布局文件,因此(第三行“输入”)。 I just give a three line quote: 我只给出三行报价:

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

So inside my \\res\\layout\\input.xml I added the reference to my popup-layout: 因此,在我的\\ res \\ layout \\ input.xml中,我添加了对我的popup-layout的引用:

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

And my \\res\\layout\\popup.xml looks like this; 我的\\ res \\ layout \\ popup.xml看起来像这样; I believe I copied it from the provided sample project. 我相信我从提供的示例项目中复制了它。 Today I just changed the two marked lines for light blue background colour and for black text colour and that finally did the trick. 今天,我只是将两条标记线更改为浅蓝色背景色和黑色文本颜色,终于成功了。 Seems I had looped references earlier but no error messages, just the ugly black default layout. 似乎我之前循环了引用,但是没有错误消息,只是丑陋的黑色默认布局。

<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"/>

The mentioned samplekeybackground.xml is just a very simple definition, pointing to two actual xml-colour-defintions: 提到的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>

And just to be complete, because I appreciate stuff I can just copy and play with for testing, here is the normal.xml ; 而且,为了完整起见 ,因为我很欣赏我可以复制并进行测试的东西,所以这里是normal.xml the pressed.xml is the same, just a darker blue: 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>

All this is from guessing and building many versions until I got lucky. 所有这些都是从猜测和构建许多版本开始,直到我幸运为止。 Can probably not answer any follow-up questions, but it does work: 可能无法回答任何后续问题,但确实有效:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM