无法在InputView中显示TabLayout

时间:2017-09-08 11:18:20

标签: java android android-tablayout android-input-method

我正在创建Android输入法,并希望使用TabLayout中支持库中的InputView(用户以键盘形式输入文本的UI)。< / p>

InputView的布局非常简单:

<?xml version="1.0" encoding="utf-8"?>
<org.szm.enigma2.ime.InputView xmlns:android="http://schemas.android.com/apk/res/android"
                               android:orientation="vertical"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content">
    <android.support.design.widget.TabLayout android:id="@+id/tab"
                                             android:layout_width="match_parent"
                                             android:layout_height="wrap_content"
                                             android:layout_gravity="center_horizontal"
                                             android:layout_alignParentTop="true"
                                             style="@style/Theme.AppCompat">
        <android.support.design.widget.TabItem android:text="Hello"/>
        <android.support.design.widget.TabItem android:text="World"/>
        <android.support.design.widget.TabItem android:text="Enigma"/>
    </android.support.design.widget.TabLayout>
</org.szm.enigma2.ime.InputView>

我对InputView和设置活动使用相同的布局,这是一项正常活动。

InputView的子类创建InputMethodService

return getLayoutInflator().inflat(R.layout.input_view, null);

在设置活动类的构造函数中:

setContentView(R.layout.input_view);

运行程序时,设置活动正常。然后标签布局工作完美。但是当我激活输入法的输入视图时,程序会崩溃并显示以下信息:

09-08 18:56:49.064 28437-28437/org.szm.enigma2 E/AndroidRuntime: FATAL EXCEPTION: main
        Process: org.szm.enigma2, PID: 28437
        android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class android.support.design.widget.TabLayout
        Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class android.support.design.widget.TabLayout
        Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
            at android.view.LayoutInflater.createView(LayoutInflater.java:652)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
            at org.szm.enigma2.ime.InputService.onCreateInputView(InputService.java:42)
            at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1228)
            at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1622)
            at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1590)
            at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:442)
            at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:206)
            at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6175)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
        Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
            at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
            at android.support.design.widget.TabLayout.<init>(TabLayout.java:297)
            at android.support.design.widget.TabLayout.<init>(TabLayout.java:291)
            at java.lang.reflect.Constructor.newInstance0(Native Method) 
            at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
            at android.view.LayoutInflater.createView(LayoutInflater.java:652) 
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812) 
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:883) 
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:522) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:430) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
            at org.szm.enigma2.ime.InputService.onCreateInputView(InputService.java:42) 
            at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1228) 
            at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1622) 
            at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1590) 
            at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:442) 
            at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:206) 
            at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:154) 
            at android.app.ActivityThread.main(ActivityThread.java:6175) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
09-08 18:56:49.081 28437-28437/? I/Process: Sending signal. PID: 28437 SIG: 9

即使我设置了style属性,我也不知道它为什么不起作用。如何在输入视图中使用制表符布局? (因为输入视图不在任何活动中)

由于

修改

我已经检查了this并搜索了其他资源。他们中的大多数都在处理在Activity中夸大TabLayout的错误。我的问题是TabLayout在Activity中运行 fine ,但在输入视图中没有。

1 个答案:

答案 0 :(得分:0)

您可以这样做:

Context context = new ContextThemeWrapper(this, android.support.v7.appcompat.R.style.Theme_AppCompat_Light_NoActionBar);
LayoutInflater inflater = LayoutInflater.from(context);
view = (View) inflater.inflate(R.layout.XXXXX, null);

Tablayout位于R.layout.XXXXX