【问题标题】:Android how to use Layout InflatorAndroid 如何使用 Layout Inflator
【发布时间】:2016-08-03 21:19:24
【问题描述】:

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

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

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

看来我必须使用 Layout Inflator 来为键盘创建一个独立的视图。但是,我不明白从 stackoverflow 帖子中提供的解决方案中该怎么做。

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

编辑:

这是我项目的文件结构:

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

}

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

我做错了什么?

【问题讨论】:

  • 链接帖子中的答案非常简单。您遇到了哪些问题?
  • @MikeM。我在我的帖子中添加了一个编辑。它为我的问题提供了更多细节。
  • inflate()调用改为使用R.id.custom_keyboard_layout,并将mInputView改为RelativeLayout
  • @MikeM。我在我的 XML 文件中添加了一个 id 属性并将 inflate 调用更改为inflate(R.id.testlayout, null),其中testlayoutcustom_keyboard_layout 的id。但是,编译器在R.id.testlayout 下划线,并显示“预期的布局资源类型”错误。此外,如果我确实将mInputView 设置为RelativeLayout,那么我无法调用mInputView.setKeyboard
  • 是的,这是一个错字。对不起。应该是R.layout.custom_keyboard_layout。此外,如果您使用自己的自定义布局,则不会使用Keyboard,因此您不需要调用该方法。

标签: android android-layout keyboard android-softkeyboard


【解决方案1】:

这是 Mike M. 在我帖子的 cmets 中提供的解决方案。

先把目录结构改成这样:

注意:我删除了 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;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多