Android-app widget (2)

Source: Internet
Author: User
Tags home screen

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.