Android learning-layout

Android layout

LinearLayout (linear layout)

RelativeLayout (relative layout)

TableLayout (table layout)

FrameLayout (frame layout)

AbsoluteLayout (absolute layout)

GridLayout (grid layout)

LinerLayout

LinerLayout mind map

Detailed explanation of weight attributes

实现代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/LinearLayout1"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    android:orientation="horizontal">   
        
    <LinearLayout    
        android:layout_width="0dp"    
        android:layout_height="fill_parent"    
        android:background="#ADFF2F"     
        android:layout_weight="1"/>    
       
        
    <LinearLayout    
        android:layout_width="0dp"    
        android:layout_height="fill_parent"    
        android:background="#DA70D6"     
        android:layout_weight="2"/>    
        
</LinearLayout> 

实现效果
weight

Divided by proportion 水平方向: Set the android: of the involved View width属性设置为0dp, and then set the proportion to the android weight attribute; by analogy 竖直方向, just set android: height为0dpand then the weight attribute!

RelativeLayout

组件属性

父容器定位属性示意图

兄弟组件定位

The so-called sibling components are components in the same level of container

Components 1 and 2 in the figure are sibling components, and component 3 and component 1 or component 2 are not sibling components, so component 3 cannot be positioned by component 1 or 2, such as layout_toleftof = "component 1", which will report an error of! Remember! The most classic example of the positioning of this brother component is the "Plum Blossom Layout".

//实现代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/RelativeLayout1"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent" >    
    
    <!-- 这个是在容器中央的 -->    
        
    <ImageView    
        android:id="@+id/img1"     
        android:layout_width="80dp"    
        android:layout_height="80dp"    
        android:layout_centerInParent="true"    
        android:src="@drawable/pic1"/>    
        
    <!-- 在中间图片的左边 -->    
    <ImageView    
        android:id="@+id/img2"     
        android:layout_width="80dp"    
        android:layout_height="80dp"    
        android:layout_toLeftOf="@id/img1"    
        android:layout_centerVertical="true"    
        android:src="@drawable/pic2"/>    
        
    <!-- 在中间图片的右边 -->    
    <ImageView    
        android:id="@+id/img3"     
        android:layout_width="80dp"    
        android:layout_height="80dp"    
        android:layout_toRightOf="@id/img1"    
        android:layout_centerVertical="true"    
        android:src="@drawable/pic3"/>    
        
    <!-- 在中间图片的上面-->    
    <ImageView    
        android:id="@+id/img4"     
        android:layout_width="80dp"    
        android:layout_height="80dp"    
        android:layout_above="@id/img1"    
        android:layout_centerHorizontal="true"    
        android:src="@drawable/pic4"/>    
        
    <!-- 在中间图片的下面 -->    
    <ImageView    
        android:id="@+id/img5"     
        android:layout_width="80dp"    
        android:layout_height="80dp"    
        android:layout_below="@id/img1"    
        android:layout_centerHorizontal="true"    
        android:src="@drawable/pic5"/>    
    
</RelativeLayout>

TableLayout

属性图

How to determine the number of rows and columns

① If we add components directly to TableLayout, then this component will occupy a row! ! !

②If we want to have multiple components on a row, we must add a TableRow container and throw all the components into it!

③The number of components in the tablerow determines how many columns the row has, and the width of the column is determined by the widest cell in the column

④The layout_width attribute of tablerow, the default is fill_parent, we set it to other values ​​will not take effect! ! ! But layout_height is wrapten——content by default, but we can set the size ourselves!

⑤The width of the entire table layout depends on the width of the parent container (to occupy the parent container itself)

⑥You have to count how many rows you have, one row per tablerow, and one row for a single component! The number of columns depends on the number of components in tableRow, and the most component is the number of columns in TableLayout

三个常用属性

android:collapseColumns: Set 被隐藏the sequence number of the required column
android:shrinkColumns: Set 被收缩the column sequence number of the allowed column
android:stretchColumns: Set 被拉伸the column sequence number of the running column

The column numbers of 从0开始算the above three attributes are all , such as shrinkColunmns = "2", corresponding to the third column!
Yes 设置多个, use, for 逗号隔开example, "0,2". If all columns are valid, use "*".
In addition to these three common attributes, there are two other attributes, namely skip grid and merged cells, which are the same as HTML The Table in is similar:

android:layout_column="2":It means to skip the second one and display it directly to the third grid, counting from 1!: It
android:layout_span="4"means 合并4 cells, that is, this component occupies 4 cells

FrameLayout

Common attributes

Foreground image: The image that is always on the top of the frame layout and directly facing the user is the image that will not be overwritten.

  • android:foreground :*Set the foreground image of the container for changing the frame layout
  • android:foregroundGravity : Set the display position of the foreground image

GridLayout

Features

  • You can set the arrangement of components in the layout yourself
  • You can customize how many rows and columns the grid layout has
  • You can directly set the component to be located in a certain row and certain column
  • You can set the component to span several rows or columns

Attributes

AbsoluteLayout

几乎不做使用,略过

Guess you like

Origin blog.csdn.net/beigucheng/article/details/113995129