Android如何使用Layout Inflator

时间:2016-04-13 02:00:18

标签: android android-layout keyboard android-softkeyboard

我正在为Android制作一个软键盘。然而,我的键盘设计非传统,这意味着我不想使用传统的Keyboard XML布局,而是想使用Relative Layout

这是我为创建输入服务而遵循的教程: http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

我正在寻找使用Relative Layout代替普通键盘布局文件的方法,我发现这个堆栈溢出帖子:
Android: Non-Keyboard IME

似乎我必须使用布局充气器为键盘创建独立视图。但是我不知道如何从stackoverflow帖子中提供的解决方案做什么。

那么有人可以解释如何使用Relative Layout文件来设计自定义键盘的键盘而不是默认的键盘布局文件吗?

编辑:

这是我项目的文件结构:

enter image description here

custom_keyboard_layout是我希望键盘使用的Relative Layout文件。

看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
<!-- This is a very rough test version of the layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.roymunson.vroy.customtestkeyboard.IntroScreen">

<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:text="Custom Button." />
</RelativeLayout>

我的Keyboard.xml文件如下所示:

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

我的customInputMethod.java文件是InputMethodService,如下所示:

    package com.roymunson.vroy.customtestkeyboard;

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.view.View;


public class customInputMethod extends InputMethodService {

    private Keyboard keyboard;

    @Override public void onInitializeInterface() {
        keyboard = new Keyboard(this, R.xml.custom_keyboard_layout);
    }

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

}

我认为这应该可行,让我使用相对布局文件作为我的自定义键盘,但事实并非如此。因为我对此真的很陌生,所以我不知道我做错了什么。

我出错了什么?

1 个答案:

答案 0 :(得分:1)

以下是Mike M.在我的帖子评论中提供的解决方案。

首先将目录结构更改为:

enter image description here

注意:我删除了keyboard.xml文件,因为它似乎没有在这种新方法中使用,但可能需要再次使用。

然后我将代码更改为:

    package com.roymunson.vroy.customtestkeyboard;

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.view.View;
import android.widget.RelativeLayout;


public class customInputMethod extends InputMethodService {

    private Keyboard keyboard;

    @Override public void onInitializeInterface() {
        keyboard = new Keyboard(this, R.layout.custom_keyboard_layout);
    }

    @Override
    public View onCreateInputView() {

        RelativeLayout mInputView = (RelativeLayout) getLayoutInflater().inflate(R.layout.custom_keyboard_layout, null);
        return mInputView;
    }

}