Android user Interface design: framework Layout

Source: Internet
Author: User
Keywords Layout Param xml

A framework layout is one of the simplest types of layout that organizes controls into the user interface of an Android program.

Understanding layouts is important for good Android programming. In this tutorial, you will learn about framework layouts, which are used primarily to organize special or overlapping view controls on the screen. With proper use, many interesting Android user interfaces can be designed based on the framework layout.

what is a frame layout

The framework layout is one of the simplest and most efficient layouts for the Android Developer organization View Control. They are used less than some other layouts, simply because they are typically used only to display a single view or overlapping views. A frame layout is often used as a container layout because it generally has only one child view (usually another layout for organizing multiple views).

&http://www.aliyun.com/zixun/aggregation/37954.html ">nbsp;

Tip: In fact, you'll see that the frame layout is used as the parent layout for any layout resource you design. If you create your program in a hierarchical View tool (hierarchy Viewer tool, a useful tool for debugging your program layout), you will find that any layout resources you design are displayed in a parent layout-a frame layout.

The frame layout is very simple, which makes them very efficient. They can be defined in an XML layout resource file, or they can be defined in a program through Java code. A child view in a frame layout is always drawn to the upper-left corner relative to the screen. If more than one child view is present, they are drawn in the order that one is stacked on top of another. This means that the first view added to the frame layout will appear at the bottom of the stack, and the last added view will appear at the top.

Let's look at a simple example. We assume that there is a frame layout resizing to control the entire screen (in other words, 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 displayed at the top of the screen. A TextView is displayed at the bottom of the screen (using the Layout_gravity property to sink the TextView to the bottom of the parent layout).

The following illustration shows what this type of layout will look like on the screen:

Define a 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 many static creation and user interface control layouts and the definition of control properties to 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 views: a picture full of the entire screen, two text controls drawn on it, and each text control is a 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=" NRC "android:gravity=" right "android:textcolor=" #fff "android:textsize=" 50DP " > </FrameLayout>

Recall that in an 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 resources are stored in the/res/layout/framed.xml file, this line of code should be:

Setcontentview (r.layout.framed);

You can also use programs to create and configure frame layouts. This is accomplished by using the Framelayout class (Android.widget.FrameLayout). You will find specific parameters in the Relativelayout.layoutparams class. Similarly, the typical layout parameters (Android.view.ViewGroup.LayoutParams), such as Layout_height and Layout_width, and margin parameters (Viewgroup.marginlayoutparams) can also be used on Framelayout objects.

Instead of using the Setcontentview () method to load a layout resource as shown earlier, you must create the screen content in Java and then provide a Setcontentview () method with a parent layout object that contains all the contents of the control to be displayed as a child view. Here, your parent layout is the frame layout. For example, the following code example illustrates how to recreate the same layout described earlier by the program. In particular, we instantiate a framelayout in the activity and add a ImageView control to 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 final screen runs exactly the same as the previous picture shows.

when to use a frame layout

When you are free to use other powerful layout types, such as linear layouts, relative layouts, and table layouts, it's 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 (the main screen, the game interface with one canvas, and so on). Sometimes other inefficient layout design can be simplified to a more efficient framework layout design, while other times it is more appropriate to use more professional layout types. Frame layout is a general choice when you want to stack views.

look at similar controls

Framelayout is comparatively 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 boundary. All home screen application gadgets are located in a frame layout.

All frame layouts need to be aware that they can also set the foreground color in addition to the usual background. This is accomplished by Android:foreground XML attributes. This can also be used for the view below the frame.

Summary

The Android Android program user interface is defined using layouts, which are 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 more than one child view exists in a frame layout, they are drawn in order and the last child control is drawn on top. [中文版]

Reprint Please specify:
Author: rockux–web Front end
From: Android user interface design: Frame layout

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.