
我想要实现的是一个不错的键盘,我可以在像素中添加键间距,其余的空间将由键共享,具体取决于我如何加权它们.
我还发现百分比宽度不准确(行结束变化-6px).
解决方法 每个键的宽度通常为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;} 唉,这不是故事的结局.键盘视图/键盘似乎决定了您将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,所有代码都是公共领域
总结以上是内存溢出为你收集整理的Android KeyboardView没有正确调整大小全部内容,希望文章能够帮你解决Android KeyboardView没有正确调整大小所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)