自定义软键盘布局尺寸

时间:2015-08-06 10:16:10

标签: android android-layout android-softkeyboard

我制作了一个自定义软键盘。

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#1B0A33"
    android:keyBackground="@drawable/pexeso_keyboard_key"
/>

Java源文件:

public class PexesoKeyboard extends InputMethodService implements
        KeyboardView.OnKeyboardActionListener {

        private KeyboardView kv;
        private Keyboard keyboard;
        private boolean caps = false;

        @Override
        public View onCreateInputView() {
            kv = (KeyboardView)getLayoutInflater().inflate(R.layout.pexeso_keyboard, null);
            keyboard = new Keyboard(this, R.xml.pexeso_keyboard_map);
            kv.setKeyboard(keyboard);
            kv.setPadding(10, 10, 10, 10);
            kv.setOnKeyboardActionListener(this);
            kv.invalidateAllKeys();
                kv.setPreviewEnabled(false);
                return kv;
            }


             ..
             ..
             ..
}

现在它看起来如此:

enter image description here

我希望它看起来如此: enter image description here

如何调整布局以及什么是最佳解决方案?欢迎所有想法

1 个答案:

答案 0 :(得分:1)

这比我想象的要容易。我只编辑了布局 - 添加了相对布局的键盘视图。然后将LayoutInflater添加到onCreateInputView()。

<强>爪哇

@Override
    public View onCreateInputView() {

        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = li.inflate(R.layout.pexeso_keyboard_layout, null);
        kvw = (KeyboardView)v.findViewById(R.id.keyboard);
        keyboard = new Keyboard(this, R.xml.pexeso_keyboard_map);
        kvw.setKeyboard(keyboard);
        kvw.setOnKeyboardActionListener(this);
        kvw.setPreviewEnabled(false);
        keyboardMinWidth = kvw.getKeyboard().getMinWidth();
        displayWidth = getApplication().getResources().getDisplayMetrics().widthPixels;
        kvw.invalidateAllKeys();
        return v;
    }

<强>布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/pxs_keyboard_background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center_vertical|bottom">

    <android.inputmethodservice.KeyboardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/keyboard"
        android:layout_margin="35dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/pxs_keyboard_background"
        android:keyBackground="@drawable/pexeso_keyboard_key"
        android:layout_centerHorizontal="true">
    </android.inputmethodservice.KeyboardView>

</RelativeLayout>

预览

How it looks