Android FrameLayout, androidframelayout

Source: Internet
Author: User

Android FrameLayout, androidframelayout

First, let's take a look at the demo:

FrameLayout framework layout is the simplest layout form. All views added to this layout are displayed in a stacked manner. The first added control is placed at the bottom layer, and the last view added to the Framework layout is displayed at the top layer. The control at the top layer overwrites the control at the next layer. This display method is similar to a stack.

When we add components to it, all components will be placed in the upper left corner of the area;

The size of the frame layout is determined by the largest child widget in the Child widget. If all the components are the same, you can only see the top widget at the same time.

1 layout_gravity

FrameLayout cannot control the position of its child control at all. The child control can control its position in the parent control through the android: layout_gravity attribute, so as to determine the method of the component.

2 layout_margin

The controls in the FrameLayout layout independently set the properties of the layout_margin class. The layout_margin control in FrameLayout must depend on the layout_gravity attribute. Otherwise, the layout_margin setting is invalid. Layout_gravity has several values that can be set. Which one can be set? In fact, layout_gravity can be understood as the reference point for setting the control. The display position of the control is ultimately determined by layout_gravity and layout_margin.

If you want to display the control properly, you can set layout_gravity of the control to top, with the upper left corner of the screen as the reference point.

3 foreground image:

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

Common attributes:

Android: foreground: sets the foreground image of the container for this frame layout.

Android: foregroundGravity: Set the foreground image display position

Frame layout is widely used in game development. When you need to write a View by yourself, you have completed your logic in the View (for example, in the game pai_^), then this View only needs to be placed in a container, you can use FrameLayout. Although other la s can also be used, the simplest method is not to save system resources.

3. Application Instances

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 view, 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" android: layout_height = "wrap_content" android: text = "The following is a FrameLayout, default layout "/> <FrameLayout android: layout_width =" match_parent "android: layout_height =" 80dp "android: background =" # ff0000 "> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "first layer" android: textColor = "#00ff00" android: textSize = "55sp"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Layer 2" android: textColor = "# 0000ff" android: textSize = "45sp"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Layer 3" android: textColor = "#00 ffff" android: textSize = "35sp"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Layer 4" android: textColor = "# ffff00" android: textSize = "25sp"/> </FrameLayout> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "The following is a FrameLayout. Adjust the location through layout_gravity and margin, and set the foreground image 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" android: layout_height = "wrap_content" android: layout_gravity = "top | left" android: layout_marginLeft = "20dp" android: layout_marginTop = "0dp" android: text = "Top left: 20 top: 0" android: textColor = "#00ff00" android: textSize = "15sp"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "top | left" android: layout_marginLeft = "20dp" android: layout_marginTop = "25dp" android: text = "Top left: 20 top: 25" android: textColor = "#00aa00" android: textSize = "15sp"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "bottom | left" android: layout_marginBottom = "20dp" android: layout_marginLeft = "45dp" android: text = "Bottom left: 45 bottom: 20" android: textColor = "# 0000ff" android: textSize = "15sp"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "center" android: text = "center" android: textColor = "#00 ffff" android: textSize = "15sp"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "right | top" android: layout_marginTop = "30dp" android: text = "top 30 on the right" 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 layer, hidden" android: textSize = "15sp"/> </FrameLayout> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "The following is a FrameLayout. Use the layout method to set the location of the Control. Click framelayou"/> <FrameLayout android: id = "@ + id/frameLayout" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "# ff0000"> <Button android: id = "@ + id/btn" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "click elsewhere"/> </FrameLayout> </LinearLayout>

  

  

My microblog: honey_11

Download Demo
Finally, the above examples all come from Android. Please download the sample source code from app Bao or pods: the source code is exhausted by the source code.

 

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.