Android自定义软键盘 - 将键盘宽度设置为match_parent(设备屏幕宽度)

时间:2015-10-12 09:11:05

标签: android android-layout android-view android-softkeyboard

我想要一个类似于图像2的软键盘以及更多按钮。

我正在关注这个例子: https://android.googlesource.com/platform/development/+/master/samples/SoftKeyboard/

但我的问题是我不知道如何设置软键盘宽度以匹配其父键设置keyWidth 15% - >我的键盘左侧是空的,如图像1。

你能救我吗?谢谢,

Class SoftKeyboard

public class SoftKeyboard extends InputMethodService 
    implements KeyboardView.OnKeyboardActionListener {

    private LatinKeyboardView mInputView; //public class LatinKeyboardView extends KeyboardView

    @Override public View onCreateInputView() {
        mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
                R.layout.input, null);
        mInputView.setOnKeyboardActionListener(this);
        setLatinKeyboard(mQwertyKeyboard);
        return mInputView;
    }


    @Override public void onInitializeInterface() {
        if (mQwertyKeyboard != null) {
            // Configuration changes can happen after the keyboard gets recreated,
            // so we need to be able to re-build the keyboards if the available
            // space has changed.
            int displayWidth = getMaxWidth();
            if (displayWidth == mLastDisplayWidth) return;
            mLastDisplayWidth = displayWidth;
        }
        mQwertyKeyboard = new LatinKeyboard(this, R.xml.qwerty);
        mSymbolsKeyboard = new LatinKeyboard(this, R.xml.symbols);
        mSymbolsShiftedKeyboard = new LatinKeyboard(this, R.xml.symbols_shift);
        mNumberKeyboard = new LatinKeyboard(this, R.xml.number);
    }

    //...
}

R.layout.input.xml:

    android:id="@+id/keyboard"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

R.xml.number.xml:

<Keyboard
android:keyWidth="15%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="@dimen/key_height"
android:layout_width="match_parent">

<Row>
    <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
    <Key android:codes="50" android:keyLabel="2"/>
    <Key android:codes="51" android:keyLabel="3"/>
    <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" android:keyEdgeFlags="right"
        android:isRepeatable="true"/>
</Row>

<Row>
    <Key android:codes="52" android:keyLabel="4" android:keyEdgeFlags="left"/>
    <Key android:codes="53" android:keyLabel="5"/>
    <Key android:codes="54" android:keyLabel="6"/>
    <Key android:codes="28" android:keyLabel="RAZ" android:keyEdgeFlags="right"/>
</Row>

<Row>
    <Key android:codes="55" android:keyLabel="7" android:keyEdgeFlags="left"/>
    <Key android:codes="56" android:keyLabel="8"/>
    <Key android:codes="57" android:keyLabel="9" android:keyEdgeFlags="right" />
</Row>

<Row android:rowEdgeFlags="bottom">
    <Key android:codes="-3" android:keyIcon="@drawable/sym_keyboard_done" />
    <Key android:codes="48" android:keyLabel="0"/>
    <Key android:codes="10" android:keyIcon="@drawable/sym_keyboard_return"
            android:keyWidth="47%p" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

在这里更改你的xml

<?xml version="1.0" encoding="utf-8"?>

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="20%p"
android:horizontalGap="6%p"
android:verticalGap="2.15%p"
android:keyHeight="@dimen/key_height"
>

图片

proper work

Thnaks