我扩展了 Android Sample SoftKeyboard 示例。这个扩展需要在键盘上方添加两个垂直的 AlphabetLinearLayouts(从 LinearLayout 扩展),除了 LatinKeyBoardView。LatinKeyboardView 是标准键盘。
当我从布局中删除这两个侧视图时,行为符合预期 - 在 EditText 内部进行选择会拉起软键盘,并且之前屏幕上的所有视图都向上移动。
通常当键盘纵向弹出时,屏幕上的任何内容都会移动到键盘上方,有效地将一些内容推离屏幕以为键盘腾出空间。
我认为因为我有这两个额外的垂直视图作为我的键盘的一部分,所以框架试图向上推送内容并失败,因为键盘视图占用了太多空间。
我试图弄清楚如何解决这个问题。最好的情况是以某种方式让我的垂直视图显示在原始内容之上,除了显示软键盘所需的内容之外没有进一步的位移。
我构造我的 KeyboardView 如下:
@Override public View onCreateInputView() {
  RelativeLayout outer = (RelativeLayout) getLayoutInflater().inflate(
        R.layout.input, null);
  // Set up keyboard view.
  mInputView = (LatinKeyboardView) outer.findViewById(R.id.keyboard);
  mInputView.setOnKeyboardActionListener(this);
  mInputView.setKeyboard(mQwertyKeyboard);
  // Maintain reference to APC.
  mAlphabetViewLeft = (AlphabetColumnLayout)outer.findViewById(R.id.alphabet_list_left);
  mAlphabetViewRight = (AlphabetColumnLayout)outer.findViewById(R.id.alphabet_list_right);
  // Set of references to handle touches.
  mAlphabetViewLeft.setTouchResolver(mTouchResolver);
  mAlphabetViewRight.setTouchResolver(mTouchResolver);
  return outer;
}
我的 input.xml 文件如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res/com.example.android.softkeyboard">
<com.example.android.softkeyboard.AlphabetColumnLayout
    android:id="@+id/alphabet_list_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_above="@+id/keyboard"
    android:orientation="vertical"
    app:available_letters="BGHIJKLMNOPUVY"/>
<com.example.android.softkeyboard.AlphabetColumnLayout
    android:id="@+id/alphabet_list_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_above="@+id/keyboard"
    android:orientation="vertical"
    app:available_letters="ACDEFQRSTWXZ"/>
<com.example.android.softkeyboard.LatinKeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</RelativeLayout>
最后,我使用以下代码设置了 AlphabetColumnLayouts。它的作用是在键盘上方屏幕的每一侧创建一堆字母(我上面提到的两个输入视图)。AlphabetTextView 是一个普通的 TextView,带有一些额外的东西来帮助我以我想要的方式解决触摸问题。char[] 字母使用 input.xml 中的 app:available_letters 样式实例化。
public void instantiateViews(char[] letters, int appSize) {
  Context context = mWeakContext.get();
  assertNotNull(context);
  // Set up layout params of parent based on the screen size available,
  // keeping XML-defined params.
  RelativeLayout.LayoutParams lp =
      (RelativeLayout.LayoutParams) getLayoutParams();
  if (lp == null) {
    lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
  }
  lp.height = appSize/2;
  Log.e(TAG, "LinearLayout = " + lp.debug(""));
  setLayoutParams(lp);
  // Set up the child TextViews.
  final int nLetters = letters.length;
  for (char c : letters) {
    AlphabetTextView tv = new AlphabetTextView(context, this);
    LinearLayout.LayoutParams tlp = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.MATCH_PARENT,
      lp.height/nLetters);
    tv.setLayoutParams(tlp);
    //tv.setGravity(Gravity.CENTER);
    tv.setText(String.valueOf(c));
    tv.setTextSize(12);
    tv.setTypeface(Typeface.DEFAULT_BOLD);
    // Add child to Layout.
    addView(tv);
  }
  setOnTouchListener(this);
}
这是我发布的 Android Groups 问题的链接,并附有一些图片。