2015-06-08 17 views
5

Tôi đã cố gắng để thêm scrollview để bàn phím tùy chỉnh Android của tôi IME, nhưng không có gì tôi đã cố gắng không làm việc cho đến nay.Làm thế nào để thêm scrollview để keyboardView trong android

Dưới đây là một phần của mã của tôi

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

tôi nhận được báo lỗi dưới đây

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

dự kiến: Di chuyển như hình ảnh bên dưới (Không thể tải lên ở đây, có vẻ như danh tiếng của tôi không đủ cho :) mà)

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

Bất kỳ trợ giúp sẽ được đánh giá cao. cảm ơn

+0

gì chính xác là vấn đề? Bạn có gặp lỗi không? Sản lượng dự kiến ​​so với đầu ra thực tế là gì? – JNYRanger

+0

Tôi có vấn đề tương tự. Nhưng tôi đã không nhận được giải pháp. –

Trả lời

2

Cố gắng làm cho sự thay đổi này:

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; 
} 
Các vấn đề liên quan