Summary: The framework layout is one of the simplest and most effective layouts for Android Developer organization View controls. With this article, you'll learn all about frame layouts, which are used primarily to organize special or overlapping view controls on the screen. If used properly, many interesting Android app user interfaces can be designed based on the frame layout.
Frame layouts are one of the simplest layout types to organize controls in the user interface of an Android program.
Understanding the layout is very important for good Android programming. In this tutorial, you will learn about the framework layout, which is used primarily to organize special or overlapping view controls on the screen. If used properly, many interesting Android app user interfaces can be designed based on the frame layout.
What is a frame layout
Frame layouts are one of the simplest and most efficient layouts for Android Developer organization View controls. They are used less than some other layouts, just because they are generally used only to display a single view, or overlapping views. A frame layout is often used as a container layout because it typically has only one child view (usually another layout that organizes multiple views).
Tip: In fact, you'll see that the frame layout is used as the parent layout of any layout resource you design. If you create your program in the Hierarchy View tool (Hierarchy Viewer tool, a useful debug utility for your program layout), you will find that any layout resources you design are displayed in a parent layout-a framework layout.
The layout of the frames is very simple, which makes them very efficient. They can be defined in an XML layout resource file, or in a program through Java code. A child view in a frame layout is always drawn relative to the upper-left corner of the screen. If there are multiple sub-views, they are drawn in the same order as one stacked on top of another. This means that the first view added to the frame layout will be displayed at the bottom of the stack, and the last view added will be displayed at the top.
Let's look at a simple example. We assume that there is a frame layout sizing to control the entire screen (in other words, both the Layout_width and Layout_height properties are set to Match_parent). We want to add three child controls to this frame layout:
- A imageview with a picture of the lake.
- A textview that appears at the top of the screen.
- A TextView that is displayed at the bottom of the screen (using the Layout_gravity property to sink TextView to the bottom of the parent layout).
show what this type of layout will look like on the screen:
Defining the frame layout in an XML resource file
The most convenient and maintainable way to design a program's user interface is to create an XML layout resource. This approach greatly simplifies the UI design process by moving the layout of many static and user-interface controls and the definition of control properties into XML instead of writing code.
The XML layout resource must be stored in the/res/layout project directory. Let's take a look at the framework layout described in the previous section. Similarly, this screen is basically a frame layout with three sub-views: A picture filled with the entire screen, two text controls drawn on it, and each text control is the default transparent background. This layout resource file is named/res/layout/framed.xml and is defined in XML as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" >
<imageview android:id= "@+id/imageview01" android:layout_height= "Fill_parent"
Android:layout_width= "Fill_parent" android:src= "@drawable/lake"
Android:scaletype= "Matrix" ></ImageView>
<textview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:textcolor= "#000"
Android:textsize= "40DP"
android:text= "@string/top_text"/>
<textview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/bottom_text"
android:layout_gravity= "Bottom" android:gravity= "right"
Android:textcolor= "#fff" android:textsize= "50DP"/> </FrameLayout>
Recall that in activity, you simply add a line of code to the OnCreate () method to load and display the layout resources on the screen. If the layout resource is stored in the/res/layout/framed.xml file, this line of code should be:
Setcontentview (r.layout.framed); To define a frame layout with a program
You can also use programs to create and configure frame layouts. This is done by using the Framelayout class (Android.widget.FrameLayout). You will find the specific parameters in the Relativelayout.layoutparams class. Similarly, typical layout parameters (Android.view.ViewGroup.LayoutParams), such as Layout_height and Layout_width, and margin parameters (Viewgroup.marginlayoutparams), which can also be used on Framelayout objects.
Instead of using the Setcontentview () method to load the layout resource directly, you must create the screen content in Java, and then provide the Setcontentview () method with a parent layout object that contains all the control content that you want to display as a child view. Here, your parent layout is the frame layout. For example, the following code example uses the program to recreate the same layout described earlier. In particular, we instantiate a framelayout in the activity and add a ImageView control in its OnCreate () method before adding two TextView controls:
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
TextView TV1 = new TextView (this);
Tv1.settext (R.string.top_text);
Tv1.settextsize (40);
Tv1.settextcolor (Color.Black);
TextView TV2 = new TextView (this);
Tv2.setlayoutparams (New Layoutparams (Layoutparams.fill_parent, Layoutparams.wrap_content, Gravity.BOTTOM));
Tv2.settextsize (50);
Tv2.setgravity (Gravity.right);
Tv2.settext (R.string.bottom_text);
Tv2.settextcolor (Color.White);
ImageView iv1 = new ImageView (this);
Iv1.setimageresource (R.drawable.lake);
Iv1.setlayoutparams (New Layoutparams (Layoutparams.fill_parent, layoutparams.fill_parent));
Iv1.setscaletype (Scaletype.matrix);
framelayout fl = new Framelayout (this);
Fl.setlayoutparams (New Layoutparams (Layoutparams.fill_parent, layoutparams.fill_parent));
Fl.addview (IV1);
Fl.addview (TV1);
Fl.addview (TV2);
Setcontentview (FL);
}
The results of the final screen are exactly the same as those shown in the previous picture.
When to use frame layouts
When you are free to use other powerful layout types, such as linear layouts, relative layouts and table layouts, it is easy to forget the frame layout. The efficiency of the frame layout makes it a good choice for screens that contain very few view controls (home screen, game interface with only one canvas, etc.). Sometimes other inefficient layout designs can be simplified to a more efficient framework layout, while others use more specialized layout types. The frame layout is a general choice when you want to stack the view.
Look at a similar control
Framelayout is relatively simple. Because of this, many other layout types and view controls are based on it. For example, ScrollView is a frame layout that appears when the child content is too large to be fully displayed within the layout bounds. All home screen app gadgets are located in a single frame layout.
For all frame layouts, it is important to note that they can also set the foreground color in addition to the usual background. This is done by android:foreground the XML attribute. This can also be used for the view below the frame.
Summarize
The Android app Android program user interface is defined using layouts, and the frame layout is one of the simplest and most efficient layout types. The child controls of the frame layout are drawn relative to the upper-left corner of the layout. If there are multiple child views in the frame layout, they are drawn sequentially and the last child control is drawn on top.
http://kb.cnblogs.com/page/98726/
Android user Interface design: Frame layout (GO)