Android Framelayout Detailed

Source: Internet
Author: User

First Look at the demo:

The Framelayout frame layout is the simplest form of layout. All views added to this layout are displayed in a cascading fashion. The first added control is placed at the bottom, and the last view added to the frame layout is displayed at the top level, and the previous control overrides the next layer of control. This display is somewhat similar to a stack.

When we add components to the inside, all the components are placed in the upper left corner of the area;

The size of the frame layout determines the largest child control in the sub-control, and if the components are all the same, only the topmost component can be seen at the same time.

1 layout_gravity

Framelayout has no control over the location of his child controls, and the child controls can control their position in the parent control through the Android:layout_gravity property, thereby making the component's way to it.

2 Layout_margin

The Framelayout layout controls individually set the properties of the Layout_margin class without effect. The control layout_margin setting in Framelayout depends on the Layout_gravity property, otherwise the Layout_margin setting is not valid. Layout_gravity There are several values you can set, specifically which one to set? In fact, layout_gravity can be understood to set the control's reference point, and the final display position of the control is ultimately determined by layout_gravity and Layout_margin.

If you want the control to display properly, you can set the control's layout_gravity to top, which is the reference point in the upper-left corner of the screen.

3 Foreground Image:

Always at the top of the frame layout, directly facing the user's image, is not overwritten by the picture

Common Properties:

Android:foreground: Sets the foreground image of the frame layout container

Android:foregroundgravity: Setting the location of the foreground image display

Frame layouts are used more in game development. When you need to write a view, you have completed your logic in view (such as the game ^_^), then the view only needs a container placement, you can use the framelayout. Although it is possible to use other layouts, the simplest is not to save the system resources.

3 Application Examples

Activity Code

Package Mm.shandong.com.testframelayout;import Android.support.v7.app.appcompatactivity;import Android.os.Bundle; Import Android.view.motionevent;import Android.view.view;import Android.widget.button;import    Android.widget.framelayout;public class Testframelayoutactivity extends appcompatactivity {framelayout FrameLayout;    Button btn;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_test_frame_layout);        BTN = (Button) Findviewbyid (R.ID.BTN);        Framelayout = (framelayout) Findviewbyid (r.id.framelayout); Framelayout.setontouchlistener (New View.ontouchlistener () {@Override public boolean onTouch (View VI                EW, motionevent motionevent) {int x = (int) motionevent.getx ();                int y = (int) motionevent.gety ();                int width = btn.getwidth ();                int height = btn.getheight (); Btn.layout (x, Y, x + width, y + height);            return false;    }        }); }}

XML code

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:orientation= "vertical" > <textview android:layout_width= "match_parent" an droid:layout_height= "Wrap_content" android:text= "Below is a framelayout, default layout"/> <framelayout android:lay            Out_width= "Match_parent" android:layout_height= "80DP" android:background= "#ff0000" > <textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "First floor "Android:textcolor=" #00ff00 "android:textsize=" 55sp "/> <textview android:l Ayout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "second layer" Androi D:textcolor= "#0000ff" anDroid:textsize= "45sp"/> <textview android:layout_width= "Wrap_content" Android:layout_h eight= "Wrap_content" android:text= "third layer" android:textcolor= "#00ffff" android:textsize= "35s P "/> <textview android:layout_width=" wrap_content "android:layout_height=" wrap_content "Android:text=" Fourth Floor "android:textcolor=" #ffff00 "android:textsize=" 25sp "/> </fr        amelayout> <textview android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:text= "Below is a framelayout that adjusts the position by layout_gravity and margin and sets the foreground picture and background color"/> <framelayout android:layout_ Width= "Match_parent" android:layout_height= "180DP" android:background= "#ff0000" android:foreground= "@ Drawable/red "android:foregroundgravity=" Right|bottom "> <button android:layout_width=" wrap_ Content "Androidoid:layout_height= "Wrap_content" android:layout_gravity= "Top|left" android:layout_marginleft= "20DP" android:layout_margintop= "0DP" android:text= "upper left left:20 top:0" android:textcolor= "#00ff00 "Android:textsize=" 15sp "/> <button android:layout_width=" Wrap_content "and             roid:layout_height= "Wrap_content" android:layout_gravity= "Top|left" android:layout_marginleft= "20DP" android:layout_margintop= "25DP" android:text= "upper left left:20 top:25" android:textcolor= "#00a            A00 "android:textsize=" 15sp "/> <button android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_gravity= "Bottom|left" Android:layout_marginbotto M= "20DP" android:layout_marginleft= "45DP" android:text= "left lower left:45 bottom:20" Android:tex        Tcolor= "#0000ff"    Android:textsize= "15sp"/> <button android:layout_width= "Wrap_content" Android:layo ut_height= "wrap_content" android:layout_gravity= "center" android:text= "center" Android:textco            Lor= "#00ffff" android:textsize= "15sp"/> <button android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_gravity= "Right|top" Android:layout_marg  intop= "30DP" android:text= "upper right Top" android:textcolor= "#ffff00" android:textsize= "15SP"            /> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_gravity= "Right|bottom" android:layout_marginbottom= "80DP" android:text= "top, Still obscured "android:textsize=" 15sp "/> </FrameLayout> <textview android:layout_width=" Match_ Parent "Android:layout_height= "Wrap_content" android:text= "Below is a framelayout, set the control location by the layout method, click framelayou"/> <framelayo UT android:id= "@+id/framelayout" android:layout_width= "match_parent" android:layout_height= "Match_par Ent "android:background=" #ff0000 "> <button android:id=" @+id/btn "Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:text= "click elsewhere"/> </framela Yout></linearlayout>

  

  

My micro-blog: honey_11

Demo download
Finally, the above examples are source with Android worry-free, please go to app bao or pea pod Download: example source code , source code example document clean sweep

Android Framelayout Detailed

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.