Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

Android: AppWidget with custom view not working

I am creating a appwidget that consists of a single custom view called Foo.

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>

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>

Foo:

  public class Foo extends View 
  {..}

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...