Create app widget Layout
You must define an initial layout for your app widget in the XML file and save it in the Res/layout directory of the project. You can use the view objects listed below to design your app widgets, but read and understand the appwidget design guide before starting the design.
If you are familiar with the layout, it is easy to create an app widget layout. However, you must note that the app widget layout is based on remoteviews, which does not support all types of layout and view.
The remoteview object supports the following layout classes:
· Framelayout
· Linearlayout
· Relativelayout
· Gridlayout
And the following visual component classes:
· Analogclock
· Button
· Chronometer
· Imagebutton
· Imageview
· Progressbar
· Textview
· Viewflipper
· Listview
· Gridview
· Stackview
· Adapterviewflipper
The derived classes of these classes are not supported.
The remoteview class also provides viewstub, which is an invisible view with a size of 0. You can use it to slowly fill the layout during runtime.
Add margins to app Widgets
Generally, Widgets should not go beyond the edge of the screen and should not be visually aligned with other widgets. Therefore, you should add margins around all the borders of your widgets.
From android4.0, app widgets automatically add margins between the widget's border and border to better align with other widgets and icons on the home screen. To obtain this benefit, we strongly recommend that you use more than 14 SDK versions for your applications.
Write a single layout that can adapt to the custom margins of the earlier version, and it is quite compatible with no additional margins in Versions later than android4.0:
1. Set the targetsdkversion attribute of the application to 14 or a later version;
2. Create the following layout to reference a size resource with margins:
<Framelayout
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: padding = "@ dimen/widget_margin">
<Linearlayout
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Orientation = "horizontal"
Android: Background = "@ drawable/my_widget_background">
...
</Linearlayout>
</Framelayout>
3. create two size resources, one in the Res/values/directory, which gives the custom margin for versions earlier than android4.0, and the other in the Res/values-v14/directory, it does not provide additional margins for widgets after android4.0.
Res/values/dimens. xml
<Dimenname = "widget_margin"> 8dp </dimen>
Res/values-v14/dimens. xml
<Dimenname = "widget_margin"> 0dp </dimen>
Another option is to create a default nine-cell background image with margins, and provide a nine-cell background image without margins for API Level 14 and later versions.