【问题标题】:How to add scrollview to keyboardView in android如何在android中将滚动视图添加到keyboardView
【发布时间】:2015-08-23 02:50:09
【问题描述】:

我一直在尝试将滚动视图添加到我的 android 自定义键盘 Ime 中,但到目前为止我还没有尝试过。

这是我的部分代码

keyboard.xml

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.inputmethodservice.KeyboardView
        android:id="@+id/keyboard"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isScrollContainer="true"
        android:scrollbarAlwaysDrawHorizontalTrack="true"
        android:scrollbarStyle="insideOverlay"
        android:scrollbars="horizontal"
        android:focusable="true"
    />
</ScrollView>

.java

 @Override
    public View onCreateInputView() {
        context = getApplicationContext();
        ScrollView scroll = (ScrollView)getLayoutInflater().inflate(R.layout.keyboard,null);
        kv = (KeyboardView)scroll.findViewById(R.id.keyboard);
        keyboard = new Keyboard(this, R.xml.qwerty);
        kv.setPreviewEnabled(false);
        kv.setKeyboard(keyboard);
        kv.setHorizontalScrollBarEnabled(true);
        kv.canScrollHorizontally(1);
        kv.setOnKeyboardActionListener(this);

        return kv;
    }

我得到下面的错误

> java.lang.IllegalStateException: The specified child already has a
> parent. You must call removeView() on the child's parent first.

预期:滚动如下图(无法在此处上传,看来我的声誉还不够:))

http://www.fandroides.com/wp-content/uploads/2014/04/Google-keyboard-emojis.png

任何帮助将不胜感激。 谢谢你

【问题讨论】:

  • 到底是什么问题?你有错误吗?预期输出与实际输出是多少?
  • 我也有类似的问题。但我没有得到解决方案。

标签: java android keyboard scrollview ime


【解决方案1】:

尝试进行此更改:

keyboard.xml

<?xml version="1.0" encoding="utf-8"?>
<android.inputmethodservice.KeyboardView
    android:id="@+id/keyboard"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
/>

my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/keyboard_layout">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="@dimen/example_height"
            android:id="@+id/keyboard_scroll">
    </ScrollView>

</LinearLayout>

.java

@Override
public View onCreateInputView() {

    LinearLayout myView = (LinearLayout) View.inflate(this, R.layout.my_layout, null);
    ScrollView scrollView = (ScrollView) myView.findViewById(R.id.keyboard_scroll);

    KeyboardView kv = (KeyboardView) getLayoutInflater().inflate(keyboard.xml, null);
    keyboard = new Keyboard(this, R.xml.qwerty);
    kv.setKeyboard(keyboard);
    kv.setOnKeyboardActionListener(this);

    scrollView.addView(kv);
    return myView;
}

【讨论】:

    【解决方案2】:
    LinearLayout myView = (LinearLayout) View.inflate(this, R.layout.my_layout, null);
    HorizontalScrollView horizontalScrollView = (HorizontalScrollView) myView.findViewById(R.id.keyboard_scroll);
    

    【讨论】:

    • 尽管这可以回答问题,但没有对您的代码的解释。请更新您的答案以解释您在做什么。谢谢!
    • Horizo​​ntallScrollView de keyboardun xml sini değiştirmek istiyorum!!
    猜你喜欢
    • 2012-07-19
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多