问题描述
我正在为Android使用自定义键盘,但遇到了一个问题,即键盘似乎在右侧留了一个白线/空格,而不是填充了父视图...(不要请注意图标,暂时只是占位符图形)
I'm working on a custom keyboard for Android, and I've run in to an issue where the keyboard seems to leave a white line/space at right, instead of filling the parent view... (don't mind the icons, it's simply placeholder graphic for now)
在下面您可以看到我的布局...
Below you can see my layout...
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="14%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="60dp"
>
<Row>
<Key android:codes="49" android:keyIcon="@drawable/rsz_emoji" android:horizontalGap="1%p" android:keyEdgeFlags="left"/>
<Key android:codes="50" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="51" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="52" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="53" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="54" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="48" android:keyIcon="@drawable/rsz_emoji" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="113" android:keyIcon="@drawable/rsz_emoji" android:horizontalGap="8%p" android:keyEdgeFlags="left"/>
<Key android:codes="114" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="116" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="121" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="111" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="112" android:keyIcon="@drawable/rsz_emoji" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="1" android:keyIcon="@drawable/rsz_emoji" android:keyWidth="28%p" android:horizontalGap="8%p" android:keyEdgeFlags="left"/>
<Key android:codes="46" android:keyIcon="@drawable/rsz_emoji" android:keyWidth="28%p"/>
<Key android:codes="58" android:keyIcon="@drawable/rsz_emoji" android:keyWidth="28%p" android:keyEdgeFlags="right"/>
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="44" android:keyIcon="@drawable/globe" android:horizontalGap="8%p" android:keyEdgeFlags="left"/>
<Key android:codes="47" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="32" android:keyIcon="@drawable/rsz_emoji" android:keyWidth="28%p"/>
<Key android:codes="1" android:keyIcon="@drawable/rsz_emoji"/>
<Key android:codes="-5" android:keyIcon="@drawable/backspace" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>
据我所知,这是1%p
的问题...但是我有点不确定如何填充它而不弄乱我的对齐方式.将第一行的间距更改为2可以解决该问题,但会弄乱对齐方式.
As far as I can see, it's a matter of 1%p
... But I'm a bit unsure how to fill it without messing up my alignments... E.g. changing the gap in the first row to 2 would fix it, but mess up the alignment.
根据要求为您添加了额外的代码...
Added extra code for you as requested...
我的课程扩展了InputMethodService
:
private KeyboardView kv;
private Keyboard keyboard;
@Override
public View onCreateInputView() {
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
keyboard = new Keyboard(this, R.xml.custom_keyboard);
kv.setPreviewEnabled(false);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);
return kv;
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection ic = getCurrentInputConnection();
if (primaryCode == Keyboard.KEYCODE_DELETE) {
ic.deleteSurroundingText(1, 0);
} else {
Drawable mDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.rsz_emoji, null);
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Emoticon", null);
Uri fileUri = Uri.parse(path);
Intent picMessageIntent = new Intent(Intent.ACTION_SEND);
picMessageIntent.setPackage("com.android.mms");
picMessageIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
picMessageIntent.setType("image/png");
picMessageIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(picMessageIntent);
}
}
@Override
public void onText(CharSequence text) {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeDown() {
}
@Override
public void swipeUp() {
}
我的Keyboard.xml
My Keyboard.xml
<?xml version="1.0" encoding="utf-8"?>
<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
Styles.xml
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="CENSORED" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Declares the input method service -->
<service android:name=".CENSORED"
android:label="@string/keyboard_name"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data android:name="android.view.im"
android:resource="@xml/method" />
</service>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
推荐答案
我无法重现该错误,我提供了一个屏幕截图,其中包含浅色背景应用程序和所有相关代码.我不确定在哪里出错了,而没有看到整个项目和目录,所以我发布了相关的xml.还有一个服务类,我没有包含(因为您显然可以使用它).
I cannot reproduce the error, I have provided a screenshot, with a light background app and all my relevant code. I am not sure exactly where you are going wrong without seeing the whole project and directories, so I've posted the relevant xml. There is a service class also, I haven't included (as you obviously have it working).
<Keyboard>
代码位于resources/xml文件夹中名为qwerty.xml
的文件中,带有名为method.xml的文件:
The <Keyboard>
code is in a file called qwerty.xml
in the resources/xml folder, with a file called method.xml:
除了替换图像外,我在qwerty.xml中使用了所有键盘尺寸.
I used all your keyboard dimensions in the qwerty.xml, except I replaced the image.
method.xml
<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android">
<subtype
android:label="@string/subtype_en_US"
android:imeSubtypeLocale="en_US"
android:imeSubtypeMode="keyboard" />
</input-method>
两个布局文件:
keyboard.xml
<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:keyPreviewLayout ="@layout/preview"
/>
preview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
android:gravity="center"
android:textSize="30sp
android:textStyle="bold">
</TextView>
样式:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
在清单中的应用程序中:
In the manifest, within application:
<service
android:name=".SimpleIME"
android:label="@string/simple_ime"
android:permission="android.permission.BIND_INPUT_METHOD"
>
<meta-data
android:name="android.view.im"
android:resource="@xml/method"/>
<intent-filter>
<action android:name="android.view.InputMethod"/>
</intent-filter>
</service>
我把图像留得太大了,所以您可以清楚地看到没有缝隙.
I've left the image so large so you can clearly see there is no gap.
此链接也将逐步介绍以下代码:
This link here also steps through this code:
http://code. tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615
修改1
在评论中进行讨论之后,我建议您检查您的背景样式,因为键盘窗口可能会显示默认背景颜色的问题/更改.此答案 https://stackoverflow.com/a/33052648/3956566 中对此问题进行了详细解决.我不会重申细节,因为该链接不太可能被腐烂,因此被贴为SO帖子.
After discussion in the comments, I would suggest checking your background style as there are issues/changes with the default background color may be showing for the keyboard window. This is addressed in detail issue in this answer https://stackoverflow.com/a/33052648/3956566. I won't reiterate the details, as the link is unlikely to rot, being an upvoted SO post.
修改2
确定在输入法服务中添加差异:
Ok Adding the difference in the input method service:
@Override
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection ic = getCurrentInputConnection();
switch(primaryCode){
case Keyboard.KEYCODE_DELETE :
ic.deleteSurroundingText(1, 0);
break;
case Keyboard.KEYCODE_SHIFT:
caps = !caps;
keyboard.setShifted(caps);
kv.invalidateAllKeys();
break;
case Keyboard.KEYCODE_DONE:
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
break;
default:
char code = (char)primaryCode;
if(Character.isLetter(code) && caps){
code = Character.toUpperCase(code);
}
ic.commitText(String.valueOf(code),1);
}
}
修改3
创建一个空白项目(键盘没有任何活动)来测试并将其作为单独的应用程序运行.您在启动器活动中使用android:theme="@style/AppTheme.NoActionBar"
,并且背景不透明度和Windows/视图可能存在问题.
Create a blank project, no activity for your keyboard, to test and run it as a separate app. You are using android:theme="@style/AppTheme.NoActionBar"
In your launcher activity and there may be an issue with background opacity and your windows/views.
这篇关于Android自定义键盘布局,侧面留有白色边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!