gpt4 book ai didi

android - KeyboardView 不采用 XML 属性 keyBackground

转载 作者:太空狗 更新时间:2023-10-29 14:20:48 26 4
gpt4 key购买 nike

我正在尝试为我的 KeyboardView 设置自定义背景。设置 XML 属性 keyBackground似乎是解决它的方法,但是当它试图膨胀可绘制对象时,我的 logcat 中不断出现 NullPointerException 斜杠 ResourceNotFoundException。我不太确定出了什么问题——所有文件似乎都在正确的位置。我将它们放在多个可绘制文件夹中,清理了我的项目等。

main.xml 文件:

<android.inputmethodservice.KeyboardView
android:id="@+id/keyboardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone"
android:keyBackground="@drawable/keybg" />

keybg.xml 文件:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_pressed="true"
android:drawable="@drawable/keybg_pressed" />
<item
android:drawable="@drawable/keybg_notpressed" />
</selector>

keybg_pressed和keybg_notpressed都是png文件

(相关)logcat:

E/AndroidRuntime(17772): FATAL EXCEPTION: main
E/AndroidRuntime(17772): java.lang.RuntimeException: Unable to start activity
ComponentInfo{MainActivity}: android.view.InflateException:
Binary XML file line #620: Error inflating class
android.inputmethodservice.KeyboardView

E/AndroidRuntime(17772): Caused by: android.content.res.Resources$NotFoundException: File
res/drawable/keybg.xml from drawable resource ID #0x7f0201a3
E/AndroidRuntime(17772): at
android.content.res.Resources.loadDrawable(Resources.java:1947)
E/AndroidRuntime(17772): at
android.content.res.TypedArray.getDrawable(TypedArray.java:601)
E/AndroidRuntime(17772): at android.inputmethodservice.KeyboardView.<init>
(KeyboardView.java:303)
E/AndroidRuntime(17772): at android.inputmethodservice.KeyboardView.<init>
(KeyboardView.java:279)
E/AndroidRuntime(17772): ... 28 more
E/AndroidRuntime(17772): Caused by: java.lang.NullPointerException
E/AndroidRuntime(17772): at
android.graphics.drawable.DrawableContainer$DrawableContainerState.
addChild(DrawableContainer.java:524)
E/AndroidRuntime(17772): at
android.graphics.drawable.StateListDrawable$StateListState.addStateSet(StateListDrawable.java:278)
E/AndroidRuntime(17772): at
android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:186)
E/AndroidRuntime(17772): at
android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
E/AndroidRuntime(17772): at
android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
E/AndroidRuntime(17772): at
android.content.res.Resources.loadDrawable(Resources.java:1944)
E/AndroidRuntime(17772): ... 31 more

最佳答案

你说“keybg_pressed 和 keybg_notpressed 都是 png 文件”。

它们应该是 XML 文件! (我知道这很奇怪)。

我将其用于我的自定义键盘 View :

    xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/keyboardView"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:keyBackground="@drawable/samplekeybackground"
android:keyTextColor="@android:color/white"
android:keyPreviewLayout="@layout/input_key_preview"
android:background="@drawable/keyboardbackground"

关于android - KeyboardView 不采用 XML 属性 keyBackground,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17158911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com