【问题标题】:Android KeyboardView does not respect key widthAndroid KeyboardView 不尊重键宽
【发布时间】:2019-08-01 20:21:57
【问题描述】:

我在运行 android 8.1.0 的 Galaxy Tab S4 和运行 android 8.0.0 的手机 Galaxy S8 之间的 KeyboardView 布局显示有不同的结果。区别在于键宽和horizo​​ntalGap。

这是我手机上正确的键盘:

在平板电脑上时,键盘如下所示:

我们可以看到第一行、第二行和第三行的键的宽度不同。但它们的大小必须相同。在平板电脑上,它似乎已调整大小以占用 100% 的父容器。同时,horizo​​ntalGap 正在将键推出父容器。

这是定义键盘布局的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="8.5%p"
    android:keyHeight="7%p"
    android:keyEdgeFlags="left">

    <Row>
        <Key android:keyLabel="Q" android:keyEdgeFlags="left" />
        <Key android:keyLabel="W" />
        <Key android:keyLabel="E" />
        <Key android:keyLabel="R" />
        <Key android:keyLabel="T" />
        <Key android:keyLabel="Z" />
        <Key android:keyLabel="U" />
        <Key android:keyLabel="I" />
        <Key android:keyLabel="O" />
        <Key android:keyLabel="P" />
        <Key android:codes="55000"  android:keyIcon="@drawable/del_keyboard" android:isRepeatable="true" android:keyWidth="15%p" android:keyEdgeFlags="right" />
    </Row>
    <Row>
        <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:keyEdgeFlags="left" android:horizontalGap="11.75%p"/>
        <Key android:keyLabel="A"  />
        <Key android:keyLabel="S" />
        <Key android:keyLabel="D" />
        <Key android:keyLabel="F" />
        <Key android:keyLabel="G" />
        <Key android:keyLabel="H" />
        <Key android:keyLabel="J" />
        <Key android:keyLabel="K" />
        <Key android:keyLabel="L" android:keyEdgeFlags="right" />
    </Row>
    <Row>
        <Key android:keyLabel=" " android:keyHeight="0px" android:keyEdgeFlags="left"  android:keyWidth="0px" android:horizontalGap="20%p"/>
        <Key android:keyLabel="Y" />
        <Key android:keyLabel="X" />
        <Key android:keyLabel="C" />
        <Key android:keyLabel="V" />
        <Key android:keyLabel="B" />
        <Key android:keyLabel="N" />
        <Key android:keyLabel="M" android:keyEdgeFlags="right" />
    </Row>
    <Row>
        <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:horizontalGap="25%p"/>
        <Key android:codes="55001"  android:keyEdgeFlags="left"  android:keyIcon="@drawable/white_space_keyboard" android:keyWidth="50%p" />
    </Row>
</Keyboard>

有关其他信息,keyboardView 位于一个包含在slidingPaneLayout 中的片段中。 有什么想法吗?提前谢谢!

【问题讨论】:

    标签: android android-layout custom-keyboard android-custom-keyboard


    【解决方案1】:

    我找到了解决方案。获取片段容器的宽度一旦可用。为所有键设置新宽度。使键无效以强制键盘重绘。对于第 2,3 和 4 行的 padding-left,我只是在每行的第一个键之前添加了一些 height = 0 的键。

    //Here we wait for the fragment width to be available
    currentView.post(new Runnable() {
            @Override
            public void run() {
                //Set the new width to the keys of the keyboard
                fixKeyboard(mKeyboard, currentView.getWidth());
                //Force redraw the keyboard
                mKeyboardView.invalidateAllKeys();
            }
        });
    //...
    
    private void fixKeyboard(Keyboard k, int dw)
    {
        List<Keyboard.Key> keys = k.getKeys();
    
        int key_width = (dw / 11) - 2; //-2 for margin-right
        int row_number = 0;
        int pos_y = 0;
        int key_index_in_row = 0;
        for (Keyboard.Key key : keys)
        {
            if (key.y != pos_y)
            {
                pos_y = key.y;
                row_number++;
                key_index_in_row = 0;
            }
    
            //Space key
            if(row_number == 3 && key_index_in_row == 3)
            {
                key.width = 5 * key_width;
            }
    
            //Delete key
            else if(row_number == 0 && key_index_in_row == 10)
            {
                key.width = key_width + 4; //Slightly larger
            }
            else
            {
                key.width = key_width;
            }
    
            key.gap = 0;
    
            key.x = key_width * key_index_in_row;
            key_index_in_row++;
        }
    }
    

    有了这个 XML

    <?xml version="1.0" encoding="utf-8"?>
    <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="8.5%p"
    android:keyHeight="7%p"
    android:keyEdgeFlags="left">
    
    <Row>
        <Key android:keyLabel="Q" android:keyEdgeFlags="left" />
        <Key android:keyLabel="W" />
        <Key android:keyLabel="E" />
        <Key android:keyLabel="R" />
        <Key android:keyLabel="T" />
        <Key android:keyLabel="Z" />
        <Key android:keyLabel="U" />
        <Key android:keyLabel="I" />
        <Key android:keyLabel="O" />
        <Key android:keyLabel="P" />
        <Key android:codes="55000"  android:keyIcon="@drawable/del_keyboard" android:isRepeatable="true" android:keyWidth="15%p" android:keyEdgeFlags="right" />
    </Row>
    <Row>
        <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:keyEdgeFlags="left"/>
        <Key android:keyLabel="A" />
        <Key android:keyLabel="S" />
        <Key android:keyLabel="D" />
        <Key android:keyLabel="F" />
        <Key android:keyLabel="G" />
        <Key android:keyLabel="H" />
        <Key android:keyLabel="J" />
        <Key android:keyLabel="K" />
        <Key android:keyLabel="L" android:keyEdgeFlags="right" />
    </Row>
    <Row>
        <Key android:keyLabel=" " android:keyHeight="0px" android:keyEdgeFlags="left"  android:keyWidth="0px"/>
        <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px"/>
        <Key android:keyLabel="Y" />
        <Key android:keyLabel="X" />
        <Key android:keyLabel="C" />
        <Key android:keyLabel="V" />
        <Key android:keyLabel="B" />
        <Key android:keyLabel="N" />
        <Key android:keyLabel="M" android:keyEdgeFlags="right" />
    </Row>
    <Row>
        <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:keyEdgeFlags="left"/>
        <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px"/>
        <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px"/>
        <Key android:codes="55001"  android:keyEdgeFlags="left"  android:keyIcon="@drawable/white_space_keyboard" android:keyWidth="50%p" />
    </Row>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 2011-04-01
      相关资源
      最近更新 更多