The ViewGroup class serves as the base for subclasses called "layouts," which offer different kinds of layout architecture, like linear, table and relative.
An Android layout is a class that handles arranging the way its children appear on the screen. Anything that is a View (or inherits from View) can be a child of a layout. All of the layouts inherit from ViewGroup (which inherits from View) so you can add views to it.
Containers/LayoutManagers are another terminologies to layouts.
The most common way to define your layout and express the view hierarchy is with an XML layout file. XML offers a human-readable structure for the layout, much like HTML. Each element in XML is either a View or ViewGroup object (or a descendant )
<? xml version = "1.0" encoding = "utf-8" ?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "vertical" > <TextView android:id = "@+id/text" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Hello, I am a TextView" /> <Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Hello, I am a Button" /> </LinearLayout>
XML layout attributes named layout_ something define layout parameters for the View that are appropriate for the ViewGroup in which it resides.
Every ViewGroup class implements a nested class that extends ViewGroup.LayoutParams . This subclass contains property types that define the size and position for each child view, as appropriate for the view group. As you can see in figure, the parent view group defines layout parameters for each child view (including the child view group).
A linearlayout respects margin s between children and the gravity (right, center, or left alignment) of each child
" layout_weight " - This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.
If we are dividing the parent in to equal parts, we just set the children’s layout_weights all to 1. But if we want to divide it unequally, we can do that in a number of ways. We can either use decimal fractional values which total 1, or we can use integer values.
To create a proportionate size layout on the screen, create a container viewgroup object with the layout_width and layout_height attributes set to fill_parent ; assign the children height or width to 0 (zero); then assign relative weight values to each child, depending on what proportion of the screen each should have.
layoutopt is a command-line tool that helps you optimize the layouts and layout hierarchies of your applications.
samples / too_many . xml 7 : 413 The root - level < FrameLayout /> can be replaced with < merge /> - 1 :- 1 This layout has too many views : 81 views , it should have <= 80 ! samples / useless . xml 7 : 19 The root - level < FrameLayout /> can be replaced with < merge /> 11 : 17 This LinearLayout layout or its FrameLayout parent is useless
The AppWidgetProvider class extends BroadcastReceiver to handle the App Widget broadcasts. The AppWidgetProvider receives only the event broadcasts that are relevant to this App Widget, such as when the App Widget is updated, deleted, enabled, and disabled.
Methods to override
onUpdate(Context, AppWidgetManager, int) - called
when each App Widget is added to a host (unless you use a configuration Activity), Typically the onlymethod that needs to be present
RemoteViews is the key mechanism behind the AppWidget feature of the Android platform.
RemoteViews is a parcelable data structure that can be sent through Android IPC (Inter Process Communication).
AppWidgets are broadcast event receivers, they process broadcasts from Launcher and respond with RemoteViews. When the RemoteViews data structure is received and deserialized, it can be applied to any ViewGroup of the UI and can therefore appear on the UI of another application (Launcher).