简体   繁体   English

如何为LinearLayout(适用于Android键盘)充气?

[英]How to inflate a LinearLayout ( for android keyboard )?

I'm trying to create an android keyboard, right now I want it just to inflate, ie show, linearlayout from main.xml 我正在尝试创建一个android键盘,现在我只希望它膨胀,即显示main.xml中的linearlayout

Here's the java file 这是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 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>

and the error it crashes with: 和它崩溃的错误:

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

Any ideas? 有任何想法吗? Thanks! 谢谢!

try below code.. 尝试下面的代码。

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

You're casting your layout, which basically is a LinearLayout , to a KeyboardView . 您正在将布局(基本上是LinearLayout投射到KeyboardView This gives you a classcast exception. 这给您一个classcast异常。 Since onCreateInputView() returns a View , it should suffice to remove the cast. 由于onCreateInputView()返回一个View ,因此足以删除演员表。 LayoutInflater.inflate() already returns a View . LayoutInflater.inflate()已经返回一个View No need to cast it to anything. 无需将其强制转换为任何东西。 ( LinearLayout extends the View class, so it's valid in this case) LinearLayout扩展了View类,因此在这种情况下有效)

So try changing 所以尝试改变

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

to

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

Alternatively you can cast it to a LinearLayout , but thats unneccesary here. 或者,您可以将其LinearLayout转换为LinearLayout ,但这在这里是不必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM