What views can i use in an appWidget?
Can anyone tell me what views can I use in an appWidget?
Thank you!开发者_JAVA百科
according to the official android API:
A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:
- FrameLayout
- LinearLayout
- RelativeLayout
- GridLayout
And the following widget classes:
- AnalogClock
- Button
- Chronometer
- ImageButton
- ImageView
- ProgressBar
- TextView
- ViewFlipper
- ListView
- GridView
- StackView
- AdapterViewFlipper
Descendants of these classes are not supported.
RemoteViews also supports ViewStub, which is an invisible, zero-sized View you can use to lazily inflate layout resources at runtime.
Also starting with android 3.0 the next views are supported:
- ListView - A view that shows items in a vertically scrolling list. For an example, see the Gmail app widget.
- GridView - A view that shows items in two-dimensional scrolling grid. For an example, see the Bookmarks app widget.
- StackView - A stacked card view (kind of like a rolodex), where the user can flick the front card up/down to see the previous/next card, respectively. Examples include the YouTube and Books app widgets.
- AdapterViewFlipper - An adapter-backed simple ViewAnimator that animates between two or more views. Only one child is shown at a time.
See the app widgets article on the Android Developers' site for a list of the layouts and views available for use in a widget.
This list provided by Christopher is technically correct, but it is not quite complete. Although these layouts and views are available, many (most?) of the presentation methods cannot be invoked. For example, if you try to programmatically change the background color of one of the supported widgets (TextView), you will receive a runtime error:
WARN/AppWidgetHostView(606): android.widget.RemoteViews$ActionException: view: android.widget.TextView can't use method with RemoteViews: setBackgroundColor(int)
I've yet to see a definitive list of what is and is not supported. The android-dev list has seen a few threads (one, two) on the topic, but not much more. The work-arounds are not so great.
精彩评论