Android:带有自定义视图的 AppWidget 不起作用 [英] Android: AppWidget with custom view not working
问题描述
我正在创建一个 appwidget,其中包含一个名为 Foo 的自定义视图.
I am creating a appwidget that consists of a single custom view called Foo.
xml/widget.xml:
xml/widget.xml:
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget_layout">
</appwidget-provider>
布局/widget_layout
layout/widget_layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<package.name.Foo
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
福:
public class Foo extends View
{..}
我在一个普通的 android 应用程序中测试了 Foo
视图,它运行良好.但是,当我尝试运行小部件时,我收到 加载小部件时出错"
.当我从小部件中删除 Foo
视图时,一切都很好.所以它与 Foo
视图有关.
I tested the Foo
view in a normal android app, and it works perfectly. However, when I try to run the widget I get "error while loading widget"
. When I remove the Foo
view from the widget, everything is fine. So it has something to do with the Foo
view.
不幸的是,我在 DDMS 中找不到任何更具体的错误,因为我不知道调试小部件的方法.
Unfortunately I can't get any more specific errors in DDMS, cause I don't know of a way to debug widgets.
我想知道是否确实可以在应用小部件中使用您自己的自定义视图?我在这里做错了吗?
I would like to know if it is indeed possible to use your own custom views in a app-widget? Am I doing something wrong here?
推荐答案
我几乎没有改变我的自定义视图,并为我的小部件实现了一个 ImageView,然后使用 getDrawingCache() 渲染视图
I pretty much left my custom view intact, and implemented an ImageView for my widget, then rendered the view using the getDrawingCache()
MyView myView = new MyView(context);
myView.measure(150,150);
myView.layout(0,0,150,150);
myView.setDrawingCacheEnabled(true);
Bitmap bitmap=myView.getDrawingCache();
remoteViews.setImageViewBitmap(R.id.dial, bitmap);
这篇关于Android:带有自定义视图的 AppWidget 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!