gpt4 book ai didi

android - 以编程方式设置 NinePatch 背景

转载 作者:太空狗 更新时间:2023-10-29 16:38:47 27 4
gpt4 key购买 nike

当我尝试以编程方式使用 9patch 背景时,

它仅在以下情况下有效:(场景 1)“拉伸(stretch)区域”(顶部/左侧)大于“内容填充区域”(底部/右侧)

但如果:(场景 2)填充比拉伸(stretch)大 - 我收到此错误:

02-08 10:32:04.203: E/AndroidRuntime(397): FATAL EXCEPTION: main
02-08 10:32:04.203: E/AndroidRuntime(397): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.game.test}: java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.os.Looper.loop(Looper.java:123)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-08 10:32:04.203: E/AndroidRuntime(397): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 10:32:04.203: E/AndroidRuntime(397): at java.lang.reflect.Method.invoke(Method.java:507)
02-08 10:32:04.203: E/AndroidRuntime(397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-08 10:32:04.203: E/AndroidRuntime(397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-08 10:32:04.203: E/AndroidRuntime(397): at dalvik.system.NativeStart.main(Native Method)
02-08 10:32:04.203: E/AndroidRuntime(397): Caused by: java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

有人知道如何解决方案 2 吗?

代码:

linLayout = new LinearLayout(this);
NinePatchDrawable ninep = (NinePatchDrawable)getResources().getDrawable(R.drawable.ninepatch_background) ;
linLayout.setBackground(ninep);

最佳答案

无需创建 NinePatchDrawable 对象。您可以简单地使用 setBackgroundResource(R.drawable.ninepatch_background)。我不知道这是否是问题所在,但我希望它能有所帮助。

关于android - 以编程方式设置 NinePatch 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21644935/

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