Android Learning Note (Android frame layout)

Source: Internet
Author: User

The Android-in-the-framelayout layout is represented by the Framelayout, which inherits the ViewGroup component directly.
The Frame layout container creates a blank area (called a frame) for each component that is added to it, and all sub-components occupy one frame, and the frames are automatically aligned according to the Gravity property. In other words, the effect of the cardlayout layout is somewhat similar to that of AWT programming, where the components are superimposed one by one. The difference with CardLayout is that cardlayout can move the card below, but Framelayout does not provide the appropriate method.
Table 2.6 shows the XML attributes commonly used in fmmelayout and the related method descriptions.
Table 2.6 Common XML attributes and related methods of Framelayout
XML attribute-related method description
Android:foreground Setforcground (DRAWABLC) set Christmas The forward image of the frame layout container
android:foregroundgravity setforegroundgravity (int) defines the gravity m nature of the front-court image


The following demonstrates the use of frame layout, the program sees 7 TextView superimposed together, above the TextView cover the TextView below. Here is the facet definition code that uses the frame layout.
Program list: Codes\02\2.2\framelay0uttest\res\layout\main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><!--define 7 TextView in turn, the first defined TextView is located at the bottom of the TextView defined by the upper--><textview android:id= "@+id/view01" Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:width= "210px" android:height= "50px" Android:background= "#ff0000"/><textview android:id= "@+id/view02" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:width= "180px" android:height= "50px" android:background= "#dd0000"/ ><textview android:id= "@+id/view03" android:layout_width= "wrap_content" android:layout_height= "wrap_content "Android:width=" 150px "android:height=" 50px "android:background=" #bb0000 "/><textview android:id=" @+id/ View04 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:width=" 120px "Android:height= "50px" android:background= "#990000"/><textview android:id= "@+id/view05" android:layout_width= "Wrap_ Content "android:layout_height=" wrap_content "android:width=" 90px "android:height=" 50px "android:background=" # 770000 "/><textview android:id=" @+id/view06 "android:layout_width=" wrap_content "android:layout_height=" wrap _content "android:width=" 60px "android:height=" 50px "android:background=" #550000 "/><textview android:id=" @+ Id/view07 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:width=" 30px "Android: height= "50px" android:background= "#330000"/> </FrameLayout>

The above interface layout definition uses a framelayout layout and adds 7 TextView to the layout container.


The height of the 7 TextView is exactly the same, and the width is gradually reduced-which ensures that the first added TextView will not be completely obscured, and we have set the background color gradient of 7 TextView.

Example: Neon effect
If you consider changing the background color of the 7 TextView in the frame layout above, you will see that the above color gradient is constantly changing, like a neon sign on the street. The following program uses the Framelayout layout manager above, but the program starts a thread to control the background color of the 7 TextView periodically. The following is the code for the main program.
Program list: Codes\02\2.2\framelayouttest\src\org\crazyit\framelayout\framelayouttest.java

Package org.crazyit.framelayout; Import Java.util.timer;import Java.util.TimerTask; Import Android.app.activity;import android.os.bundle;import Android.os.handler;import Android.os.Message;import Android.widget.TextView; /** * Description: * <br/>site: <a href= "http://www.crazyit.org" >crazyit.org</a> * <br/> Copyright (C), 2001-2012, Yeeku.h.lee * <br/>this program was protected by copyright laws.  * <br/>program Name: * <br/>date: * @author Yeeku.h.lee [email protected] * @version 1.0 */public Class Framelayouttest extends activity{private int currentcolor = 0;//define an array of colors final int[] colors = new int[] {R.color.colo R7, R.color.color6, R.color.color5, R.color.color4, R.color.color3, R.color.color2, R.color.color1,}; Final int[] names = new int[] {r.id.view01, r.id.view02, r.id.view03, r.id.view04, R.id.view05, R.id.view06, R.id. VIEW07}; Textview[] views = new Textview[7]; @Override public void OnCreate (Bundle savedinstAncestate) {super.oncreate (savedinstancestate);   Setcontentview (R.layout.main);  for (int i = 0; i < 7; i++) {views[i] = (TextView) Findviewbyid (Names[i]);    } final Handler Handler = new Handler () {@Override public void handlemessage (message msg) {//indicates that the message was sent from this program if (msg.what = = 0x1122) {//change 7 TextView background color for (int i = 0; i < 7-currentcolor; i++) {views[     I].setbackgroundresource (Colors[i + currentcolor]);     } for (int i = 7-currentcolor, j = 0; i < 7; i++, J + +) {Views[i].setbackgroundresource (colors[j]);   }} super.handlemessage (msg);  }  }; Defines a thread that periodically changes the value of the CurrentColor variable New Timer (). Schedule (new TimerTask () {@Override public void run () {CurrentColor    ++;    if (CurrentColor >= 6) {currentcolor = 0;    }//Send a message to notify the system to change the background color of 7 textview components Message m = new Message ();    Define an identity for the message m.what = 0x1122;    Handler.sendmessage (m);  }}, 0, 100); }}


The bold code in the above program defines a task that executes once every 0.1 seconds, which only changes the value of the CurrentColor variable, and then sends a message to handler notifying it to update the background color of the 7 TextView.
There may be questions from readers: Why not just update the background color of the 7 TextView directly in the Nm0 method? This is because Android view and UI components are not thread-safe, so Android does not allow shipowner to initiate threads to access UI components in the user interface. So an extra handler is defined in the program to handle updates to the TextView background color.
The above program directly uses the R.COLOR.COLOR7, R.COLOR.COLOR6, R.color.color5 and other integer represented to represent the color, which is also due to the Android resource access support, this book will have a detailed description of the color resources.
To put it simply, the above program controls the 0.1-second rotation of 7 TextView of background color every interval, so it looks like "neon" on the street.


Original address: http://www.itmmd.com/201412/271.html
This article by Meng Meng's IT person to organize the release, reprint must indicate the source.

Android Learning Note (Android frame layout)

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.