簡體   English   中英

Android:自定義視圖不起作用的AppWidget

[英]Android: AppWidget with custom view not working

我正在創建一個appwidget,它由一個名為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>

布局/ 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視圖,它運行得很好。 但是,當我嘗試運行小部件時,我收到"error while loading widget" 當我從小部件中刪除Foo視圖時,一切都很好。 所以它與Foo視圖有關。

不幸的是,我無法在DDMS中獲得任何更具體的錯誤,因為我不知道調試小部件的方法。

我想知道是否確實可以在app-widget中使用您自己的自定義視圖? 我在這里做錯了嗎?

我幾乎完全保留了自定義視圖,並為我的小部件實現了ImageView,然后使用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);

另一種不使用getDrawingCache()

MyView myView = new MyView(this);
myView.measure(500, 500);
myView.layout(0, 0, 500, 500);
Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
myView.draw(new Canvas(bitmap));
remoteViews.setImageViewBitmap(R.id.imageView, bitmap);

我使用緩存不重繪所有視圖,所以我無法使用上面的代碼。 而且我發現它更優雅。 我希望它對某人有用。

文檔

支持的視圖包括AnalogClock,Button,Chronometer,ImageButton,ImageView,ProgressBar和TextView。 對於布局,您必須使用FrameLayout,LinearLayout或RelativeLayout。

您不能在窗口小部件中使用任何自定義視圖。 事實上,即使是那些Android預定義的視圖也不是全部支持。

有關支持的小部件/布局的詳細列表,請閱讀文檔 除了記錄的內容之外的任何內容都不能放在小部件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM