2011-08-30 72 views
0

我試圖創建一個Android鍵盤,現在我希望它只是膨脹,即顯示,從main.xml中如何膨脹LinearLayout(用於Android鍵盤)?

這裏的LinearLayout是java文件

package keyboard.rob.com; 

import ... 

public class zyz extends InputMethodService 
implements KeyboardView.OnKeyboardActionListener { 

    private KeyboardView mInputView; 

    @Override public void onCreate() { 
     super.onCreate(); 
    } 

    @Override public View onCreateInputView() { 
     mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.main, null); 
     return mInputView; 
    } 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 


<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="center" android:id="@+id/LL_whole" android:orientation="vertical" android:clickable="false"> 


    <LinearLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@layout/backrep" android:layout_gravity="center" android:id="@+id/LL_total" android:orientation="vertical" android:layout_height="wrap_content"> 
     <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:background="@drawable/spell_frame" android:layout_marginBottom="10px" android:layout_marginTop="5px" android:layout_gravity="center" android:id="@+id/LL_spell" android:paddingLeft="10px"> 
     </LinearLayout> 


     <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" android:layout_width="wrap_content" android:id="@+id/LLrow1"> 

     </LinearLayout> 
     <LinearLayout android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" android:layout_width="wrap_content" android:id="@+id/LLrow2"></LinearLayout> 
     <LinearLayout android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" android:layout_width="wrap_content" android:id="@+id/LLrow3"></LinearLayout> 
     <LinearLayout android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" android:layout_width="wrap_content" android:id="@+id/LLrow4"> 
     </LinearLayout> 

    </LinearLayout> 


</LinearLayout> 

和錯誤它崩潰了:

08-29 20:21:28.128: ERROR/AndroidRuntime(10227): java.lang.ClassCastException: android.widget.LinearLayout 

任何想法?謝謝!

回答

6

試試下面代碼..

package keyboard.rob.com; 

import ... 

public class TotalKeyboard extends InputMethodService 
implements KeyboardView.OnKeyboardActionListener { 

    private LinearLayout mInputView; 

    @Override public void onCreate() { 
     super.onCreate(); 
    } 

    @Override public View onCreateInputView() { 
     mInputView = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null); 
     return mInputView; 
    } 
} 
+0

似乎工作,謝謝! – Roger

+0

@NiranjPatel我們可以使用自定義鍵盤中的LIstview/Recyclview ..Plz help..Plz爲我提供了一些鏈接/資源..謝謝 –

5

您正在將您的佈局(基本上是LinearLayout)轉換爲KeyboardView。這給你一個classcast異常。由於onCreateInputView()返回一個View,它應該足以刪除演員。 LayoutInflater.inflate()已經退貨View。無需將其投射到任何東西。 LinearLayout擴展了View類,所以它是有效的在這種情況下)

所以嘗試改變

mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.main, null); 

mInputView = getLayoutInflater().inflate(R.layout.main, null); 

或者你可以把它轉換爲LinearLayout,但多數民衆贊成unneccesary這裏。