gpt4 book ai didi

Android KeyboardView 大小不正确

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:19 24 4
gpt4 key购买 nike

我最近遇到了 Android 键盘 View 的问题。

我想要实现的是一个像样的键盘,我可以在其中添加以像素为单位的键间距,其余空间将由键共享,具体取决于我对它们的加权方式。

我还发现百分比宽度不准确(行尾变化 +-6 像素)。

最佳答案

每个键都有一个宽度,通常为 64 像素。对于半正常宽度的键,我会分配 32px。对于双倍,我给了它 128px。等等

然后键盘通过一个名为“fixKeyboard”的函数运行,该函数决定每个键的宽度。您告诉它每行有多少个 64px 宽度的键,它会将键缩放到合适的大小。 (即 keysPerStandardRow == 键盘有多少像素宽/64px)

private void fixKeyboard(Keyboard k, int keysPerStandardRow)
{
List<Key> keys = k.getKeys();
int dw = GlobalHelperFunctions.getDisplay(this).getWidth();
int ttly = 0;
int divisor = 64 * keysPerStandardRow;
int ttl_weights = 0;
for (Key key : keys)
{
//See below for the deal with 424242
int weight = key.width + (key.gap == 424242 ? 0 : key.gap);
if (key.gap == 424242)
key.gap = 0;
else
key.gap = (ttl_weights + key.gap) * dw / divisor - ttl_weights * dw / divisor;
key.width = (ttl_weights + key.width) * dw / divisor - ttl_weights * dw / divisor;
if (key.y != ttly)
{
ttl_weights = 0;
ttly = key.y;
}
key.x = ttl_weights * dw / divisor;
ttl_weights += weight;
}
}

...

static public Display GlobalHelperFunctions.getDisplay(Context c)
{
if (c != null)
{
WindowManager wm = (WindowManager)c.getSystemService(Context.WINDOW_SERVICE);
if (wm != null)
{
return wm.getDefaultDisplay();
}
}
return null;
}

唉,这还没有结束。似乎键盘 View /键盘在您将 XML 文件交给它时就决定了键盘的宽度。如果出于某种原因你像我一样更改了键,android 会将它们缩放回其预先计算的框中。显然,我不想要这个。所以这就是我所做的:我只是通过在每个键 (424242px) 之间定义一个巨大的水平间隙来强制它具有最大可能的宽度,然后在运行 fixKeyboard 时将其重置为 0。

由于我使用的是这种方法,您只需不使用触发器编号即可轻松定义间隙!这是我的 QWERTY 键盘,如果您愿意,可以使用它作为示例:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyHeight="80px"
android:horizontalGap="424242px"
android:verticalGap="2px" >
<Row android:keyWidth="64px">
<Key android:keyLabel="1" android:keycode="KEYCODE_1"/>
<Key android:keyLabel="2" android:keycode="KEYCODE_2"/>
<Key android:keyLabel="3" android:keycode="KEYCODE_3"/>
<Key android:keyLabel="4" android:keycode="KEYCODE_4"/>

<Key android:keyLabel="5" android:keycode="KEYCODE_5"/>
<Key android:keyLabel="6" android:keycode="KEYCODE_6"/>
<Key android:keyLabel="7" android:keycode="KEYCODE_7"/>
<Key android:keyLabel="8" android:keycode="KEYCODE_8"/>

<Key android:keyLabel="9" android:keycode="KEYCODE_9" />
<Key android:keyLabel="0" android:keycode="KEYCODE_0"/>
<Key android:keyIcon="@android:drawable/ic_input_delete" android:keyOutputText="◁" android:keyWidth="128px" android:codes="0x25C1" android:isRepeatable="true"/>
</Row>
<Row android:keyWidth="64px">
<Key android:keyLabel="q" android:keycode="KEYCODE_Q" />
<Key android:keyLabel="w" android:keycode="KEYCODE_W"/>
<Key android:keyLabel="e" android:keycode="KEYCODE_E" />
<Key android:keyLabel="r" android:keycode="KEYCODE_R" />

<Key android:keyLabel="t" android:keycode="KEYCODE_T"/>
<Key android:keyLabel="y" android:keycode="KEYCODE_Y" />
<Key android:keyLabel="u" android:keycode="KEYCODE_U"/>
<Key android:keyLabel="i" android:keycode="KEYCODE_I" />

<Key android:keyLabel="o" android:keycode="KEYCODE_O" />
<Key android:keyLabel="p" android:keycode="KEYCODE_P" />
<Key android:keyLabel="・"/>
<Key android:keyIcon="@android:drawable/ic_menu_preferences" android:keyOutputText="●" android:codes="0x25CF"/>
</Row>
<Row android:keyWidth="64px">
<Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:horizontalGap="32px"/>
<Key android:keyLabel="a" android:keycode="KEYCODE_A"/>
<Key android:keyLabel="s" android:keycode="KEYCODE_S"/>
<Key android:keyLabel="d" android:keycode="KEYCODE_D"/>

<Key android:keyLabel="f" android:keycode="KEYCODE_F" />
<Key android:keyLabel="g" android:keycode="KEYCODE_G"/>
<Key android:keyLabel="h" android:keycode="KEYCODE_H"/>
<Key android:keyLabel="j" android:keycode="KEYCODE_J" />

<Key android:keyLabel="k" android:keycode="KEYCODE_K" />
<Key android:keyLabel="l" android:keycode="KEYCODE_L" />
<Key android:keyLabel="ENTER" android:keyOutputText="◒" android:keyWidth="96px"/>
<Key android:keyLabel="カタ" android:keyOutputText="◎" android:codes="0x25CE"/>
</Row>
<Row android:keyWidth="64px">
<Key android:keyLabel="「" />
<Key android:keyLabel="z" android:keycode="KEYCODE_Z"/>
<Key android:keyLabel="x" android:keycode="KEYCODE_X"/>
<Key android:keyLabel="c" android:keycode="KEYCODE_C"/>

<Key android:keyLabel="v" android:keycode="KEYCODE_V" />
<Key android:keyLabel="b" android:keycode="KEYCODE_B"/>
<Key android:keyLabel="n" android:keycode="KEYCODE_N"/>
<Key android:keyLabel="m" android:keycode="KEYCODE_M"/>

<Key android:keyLabel="、"/>
<Key android:keyLabel="。"/>
<Key android:keyLabel="⇧" android:keycode="KEYCODE_SHIFT_RIGHT" android:isModifier="true"/>
<Key android:keyLabel="HW" android:keyOutputText="◍" android:codes="0x25CD"/>
</Row>
<Row android:keyWidth="64px">
<Key android:keyLabel=" " android:keyWidth="704px"/>
<Key android:keyLabel="ひら" android:keyOutputText="◐" android:codes="0x25D0"/>
</Row>
</Keyboard>

一些重要的事情:为了在键之前留出间隙,我只是添加了一个 0 宽度的键。 <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:horizontalGap="32px"/>

另一件事:对于控制键(就像打开 IME 菜单的那个,我使用特殊字符来提醒代码:android:keyOutputText="◐" 等。

我花了一段时间才找到打开 IME 菜单的方法,下面是方法: How to set/call an new input method in Android

HTH,所有代码都是公共(public)领域

关于Android KeyboardView 大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14699725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com