簡體   English   中英

Android如何使用Layout Inflator

[英]Android how to use Layout Inflator

我正在為Android開發軟鍵盤。 但是,我的鍵盤設計不是非常傳統的,這意味着我不想使用傳統的Keyboard XML Layout,而是想使用Relative Layout

這是我為了創建輸入服務而遵循的教程: http : //code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

我正在尋找使用Relative Layout而不是普通的鍵盤布局文件的方法,我發現了這個堆棧溢出帖子:
Android:非鍵盤輸入法

看來我必須使用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;
    }

}

我認為這應該可行,並且讓我使用自定義鍵盤的相​​對布局文件,但事實並非如此。 由於我真的很陌生,所以我不知道自己在做什么錯。

我怎么了?

這是Mike M.在我的帖子評論中提供的解決方案。

首先將目錄結構更改為此:

在此處輸入圖片說明

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM