安卓:AppWidget与自定义视图不工作 [英] Android: AppWidget with custom view not working
问题描述
我创建一个appwidget,由被称为富单个自定义视图。
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应用程序,它完美的作品。然而,当我尝试运行我得到的小部件错误而载入窗口小部件
。当我从窗口小部件删除富
看法,一切都很好。因此,有事情做与富
视图。
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?
推荐答案
我pretty的多少离开了我的自定义视图完好无损,并实现了一个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);
这篇关于安卓:AppWidget与自定义视图不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!