【问题标题】:Android custom soft keyboard - set keyboard width to match_parent (device screen width)Android 自定义软键盘 - 将键盘宽度设置为 match_parent(设备屏幕宽度)
【发布时间】:2015-10-12 09:11:05
【问题描述】:

我想要一个像图 2 那样带有更多按钮的软键盘。

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

但我的问题是我不知道如何设置软键盘宽度以匹配其父级设置 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
  • 当时,我确实放弃了,但你可以尝试在开头和最后一个按钮手动设置 android:horizo​​ntalGap="5%p" (用于 5% 填充)。如果这解决了您的问题,请通过发布新答案与我们保持联系。

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


【解决方案1】:

在此处更改您的 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"
>

图片

感谢

【讨论】:

    猜你喜欢
    • 2019-01-20
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 2014-02-09
    • 2014-01-10
    • 2019-10-23
    相关资源
    最近更新 更多