Android development four major layout study summary

1: There are five layout methods for android, namely: LinearLayout (linear layout), FrameLayout (single frame layout), RelativeLayout (relative layout), AbsoluteLayout (absolute layout)

(1) LinearLayout (linear layout)
linear layout: horizontal and vertical display. If there are multiple components that exceed the screen size, they will not be displayed. The direction can be defined by android:orientation. android:orientation="horizontal" means the horizontal direction
android:orientation="vertical" means the vertical direction

<LinearLayout xmlns:android="
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//android:layout_gravity="top"
android:text="Button1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//android:layout_gravity="center_vertical"
android:text="Button2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//android:layout_gravity="bottom"
android:text="Button3"/>
<LinearLayout/>

(2) Relative layout: RelativeLayout
is a layout that is usually used more often.
Left-aligned: android:layout_alighParentLeft

Right alignment: android:layout_alighParentRight

Top alignment: android:layout_alighParentTop

Bottom alignment: android:layout_alighParentBottom

Horizontally centered: android:layout_centerHorizontal

Vertically centered: android:layout_centerVertical

Central location: android:layout_centerInParent

<RelativeLayout xmlns:android="
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button3"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Button4"/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="Button5"/>
<RelativeLayout/>

Insert picture description here

(3) Frame layout: FrameLayout
is much simpler than the other two layouts, it has no convenient positioning method, all the controls will be displayed in the upper left corner)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#FF6143" />
    <TextView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#7BFE00" />
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FFFF00" />

</FrameLayout>

Insert picture description here
(4) AbsoluteLayout absolute layout
Absolute layout is like div specifies the absolute attribute, use X, Y coordinates to specify the position of the element
android:layout_x="20px"
android:layout_y="12px"
This layout method is also relatively simple, but in the vertical When switching randomly, problems often occur, and when there are multiple elements, calculations are more troublesome.

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="20dip"
        android:layout_y="20dip"
        android:text="用户名:" />
   

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="80dip"
        android:layout_y="15dip"
        android:width="200px" />
    

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="20dip"
        android:layout_y="80dip"
        android:text="密  码:" />
    

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="80dip"
        android:layout_y="75dip"
        android:password="true"
        android:width="200px" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="130dip"
        android:layout_y="135dip"
        android:text="登   录" />

   
</AbsoluteLayout >

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44941105/article/details/115221094