簡體   English   中英

如何更改Android軟鍵盤彈出鍵盤的背景顏色?

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

我花了很長時間才弄清楚如何擺脫難看的黑色默認設置並為自定義鍵盤上色。

我從這個非常有幫助的答案開始工作,現在可以很好地鍵盤着色: 如何更改android軟鍵盤按鍵的背景顏色?

只是彈出鍵盤仍為默認顏色。

我找到了另一個有用的答案,這幾乎使我找到了解決方案。 但是答案是集中在彈出窗口的創建和預覽上: 創建每個鍵具有多個/備用字符的軟鍵盤

@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商店免費提供。 有用。 但是,人們由於令人敬畏的色彩設計而猶豫不決。 一旦彈出式窗口變色,我將發布一個新版本。 謝謝。

由於兩個月來沒有任何答案,我花時間進行更多的猜測和猜測。 現在,我今天很幸運,並希望與下一位語言學家融洽相處,后者也需要自定義鍵盤並且需要通過示例進行工作:

mykeyboard.java指向主鍵盤的布局文件,因此(第三行“輸入”)。 我只給出三行報價:

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

因此,在我的\\ 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 -->
/>

我的\\ 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>

所有這些都是從猜測和構建許多版本開始,直到我幸運為止。 可能無法回答任何后續問題,但確實有效:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM