2015-11-09 76 views
10

我正在为Android设计一个自定义键盘,而且我遇到了键盘似乎在右侧留下白线/空白的问题填充父视图...(不介意的图标,它只是一个占位符图形现在)Android自定义键盘布局在侧边留下白边

Screenshot from the simulator

下面你可以看到我的布局......

<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会解决它,但搞乱了对齐。

为你添加额外的代码,请...

我的类扩展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

<?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

<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> 
+0

如何自定义键盘! –

+0

@tinysunlight那是一个答案吗? :/ – user969043

+0

因为我从来没有看到键盘标签! –

回答

2

在你的情况下:图7(键)×14%= 98%+ 1个%间隙(firstkeyśhorizontalGap)= 99%。

将最后一个键中的'%1'放入horizontalGap中,就像您在第一个键中所做的一样。

<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:horizontalGap="1%p" 
      android:keyEdgeFlags="right"/> 
</Row> 
+0

这会在最后一个键的左侧添加间隔... – user969043

+0

是的。 ..将填补右边的“白色空间”... – Christian

+0

是的,但最后一个键左侧的缺口会毁掉用户界面...因为缺口应该在右边......这个会在第二行和第一行的最后一个按钮之间添加一个丑陋的空间... – user969043

2

您是否尝试将此项添加到xml文件中的键盘标记中?

android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
+0

同样的结果不幸 – user969043

+0

我会看到它。 – sayan

7

我无法重现错误,我提供了一个截屏,一个浅色背景应用程序和所有我的相关代码。如果没有看到整个项目和目录,我不确定你到底发生了什么,所以我发布了相关的xml。还有一个服务类,我没有包括(因为你显然有它的工作)。

<Keyboard>代码是在一个名为在资源/ XML文件夹qwerty.xml文件,一个叫method.xml文件:

我用你所有的键盘尺寸在qwerty.xml,除了我替换的图像。

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> 

在清单中,应用程序中:

<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> 

我已经离开了形象如此之大,所以你可以清楚地看到没有缝隙。 enter image description here

这里这个链接也一步一步地验证码:

http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

编辑1

在评论中讨论后,我会建议,因为有问题,检查你的背景风格/可能会显示键盘窗口的默认背景色的更改。这是在这个答案https://stackoverflow.com/a/33052648/3956566详细问题。我不会重申细节,因为这个链接不太可能腐烂,因为这是一个高调的SO帖子。

编辑2

确定添加的输入法服务的区别:

@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 /视图问题。

+0

我实际上遵循了在键盘上发布链接的教程,但我的键盘和method.xml与您的键盘相同,除了我不使用键盘的预览...所以我在考虑错误不知何故是在键盘布局xml文件,我发布在我的问题...我不确定,为什么你不能重现,因为教程是我跟着的确切的一个,我唯一的区别是我的布局.. 。现在,我已经通过改变按钮尺寸来解决问题了,但这并不是我想要解决的问题... – user969043

+0

@ user969043对所有的问题都抱歉,我回到了绘图板,你有没有检查主题?主题的默认背景窗口/视图和键盘的样式之间可能会有冲突。我使用与键盘布局完全相同的xml。并注意我将它命名为qwerty.xml。 –

+0

我已经在问题中更新了我的代码... – user969043

0

我也有这个问题。我从未找到一个好的解决方案。

Android的键盘布局API很糟糕。如果我再一次这样做,我就不会使用它,而是直接创建自己的用户界面,从头开始与InputMethod接口。

在任何情况下,我的解决方案是在布局的底部添加一个额外的键,宽度为100%,高度仅为1dp。

该按钮没有任何功能,高度非常小,用户看不到它。但是关键的宽度解决了差距问题。

1

在第一行之前添加一行,宽度为100%,零高度。 是这样的。第一行之前添加以下代码

<Row android:horizontalGap="0%p" android:keyHeight="0.01%p"> 
 
     <Key android:codes="32" android:keyLabel="" 
 
      android:keyWidth="100%p" 
 
      /> 
 

 
    </Row>

0

如果keyboardview是在布局,确保填充参数设定为0dp。

+0

你可以在评论中提问 –