Android自定义软键盘-设置键盘宽度以匹配父设备(设备屏幕宽度)

Android自定义软键盘-设置键盘宽度以匹配父设备(设备屏幕宽度),android,android-layout,android-view,android-softkeyboard,Android,Android Layout,Android View,Android Softkeyboard,我想要一个像图2那样的软键盘,再加上一些按钮 我遵循这个例子: 但我的问题是,我不知道如何设置软键盘宽度,使其与设置keyWidth 15%->的父项匹配。我的键盘左侧是空的,如图1所示 你能帮我吗?谢谢 类软键盘 public class SoftKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private LatinKeyboardView m

我想要一个像图2那样的软键盘,再加上一些按钮

我遵循这个例子:

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

你能帮我吗?谢谢

类软键盘

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>

在这里更改您的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"
>

图像


Thnaks

问题不清楚添加数字xml当时,我确实放弃了,但您可以尝试手动设置android:horizontalGap=“5%p”(用于5%填充)的开头和最后一行按钮。如果这解决了你的pb问题,请通过发布新的答案来保持我们的联系。