Java 如何为线性布局充气(适用于android键盘)?

Java 如何为线性布局充气(适用于android键盘)?,java,android,Java,Android,我正在尝试创建一个android键盘,现在我只想让它膨胀,即显示main.xml中的linearlayout 这是java文件 package keyboard.rob.com; import ... public class zyz extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private KeyboardView mInputView; @Overrid

我正在尝试创建一个android键盘,现在我只想让它膨胀,即显示main.xml中的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>

有什么想法吗?谢谢

您正在将布局(基本上是
线性布局
)转换为
键盘视图
。这将为您提供一个classcast异常。由于
onCreateInputView()
返回一个
视图
,因此删除强制转换就足够了
LayoutInflater.flate()
已返回一个
视图
。没有必要把它抛给任何东西。(
LinearLayout
扩展了
视图
类,因此在这种情况下它是有效的)

所以试着改变

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


或者,您可以将其转换为
线性布局
,但这在这里是不必要的。

您正在将布局(基本上是
线性布局
)转换为
键盘视图
。这将为您提供一个classcast异常。由于
onCreateInputView()
返回一个
视图
,因此删除强制转换就足够了
LayoutInflater.flate()
已返回一个
视图
。没有必要把它抛给任何东西。(
LinearLayout
扩展了
视图
类,因此在这种情况下它是有效的)

所以试着改变

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

或者,您可以将其转换为
线性布局
,但这在这里是不必要的。

请尝试下面的代码

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;
    }
}
请尝试下面的代码

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

@NiranjPatel我们可以在自定义键盘内使用LIstview/RecycleView..Plz帮助..Plz为我提供一些链接/资源。。Thanks@NiranjPatel我们可以在自定义键盘内使用LIstview/RecycleView吗..请提供帮助..请提供一些链接/资源..谢谢