安卓六大布局

作者: 巴巴呀呀 | 来源:发表于2018-04-05 17:32 被阅读0次

    友情提示:请尽量收藏后使用pc端阅读该文档,使用markdown,代码在pc端有代码高亮,手机端不支持

    1、线性布局(LinearLayout):按照垂直或者水平方向布局的组件
    2、帧布局(FrameLayout):组件从屏幕左上方布局组件
    3、表格布局(TableLayout):按照行列方式布局组件
    4、绝对布局(AbsoluteLayout):按照绝对坐标来布局组件
    5、相对布局(RelativeLayout):相对其它组件的布局方式
    6、约束布局 (ConstraintLayout):按照约束布局组件

    线性布局

    线性布局效果图
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"    
        android:layout_height="match_parent"    
        android:orientation="vertical">    
    
    <LinearLayout        
        android:layout_width="match_parent"        
        android:layout_height="250dp"        
        android:orientation="horizontal">        
        <TextView           
            android:layout_width="96dp"      
            android:layout_height="match_parent" 
            android:background="#b2dfdb" />        
        <TextView            
            android:layout_width="96dp"               
            android:layout_height="match_parent"             
            android:background="#80cbc4" />       
         <TextView            
            android:layout_width="96dp"                   
            android:layout_height="match_parent"            
            android:background="#4db6ac" />        
        <TextView            
            android:layout_width="96dp"            
            android:layout_height="match_parent"            
            android:background="#26a69a" />    
    </LinearLayout>
        
    <LinearLayout        
        android:layout_width="match_parent"            
        android:layout_height="match_parent"        
        android:orientation="vertical">        
        <TextView           
            android:layout_width="match_parent"            
            android:layout_height="68dp"            
            android:background="#b2dfdb" />        
        <TextView            
            android:layout_width="match_parent"                
            android:layout_height="68dp"            
            android:background="#80cbc4" />        
        <TextView            
            android:layout_width="match_parent"                
            android:layout_height="68dp"            
            android:background="#4db6ac" />        
        <TextView            
            android:layout_width="match_parent"                    
            android:layout_height="68dp"            
            android:background="#26a69a" />    
    </LinearLayout>
    
    </LinearLayout>
    

    线性布局特点,同一级组件之间没有互相依赖,按照添加顺序依次排列,其中线性布局最重要的属性是android:orientation,通过该属性可以指定水平方向排列还是纵向排列

    帧布局

    帧布局效果图
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/FrameLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:background="#FF6143" />
    
        <TextView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:background="#7BFE00" />
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:background="#FFFF00" />
    
    </FrameLayout>
    

    帧布局会按照添加顺序层叠在一起,默认层叠在左上角位置,本例子因为设置了layout_gravity="center",所以层叠在视图中心位置

    表格布局

    表格视图效果图
    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:shrinkColumns="0,2"
        >
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我占据一行" />
    
        <TableRow>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0" >
            </Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="111111111111111111111111" >
            </Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="222222222222222222222222" >
            </Button>
        </TableRow>
    
        <TableRow>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="000000000000000000000000" >
            </Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="11" >
            </Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="222222222222222222222222" >
            </Button>
        </TableRow>
    
        <TableRow>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="000000000000000000000000" >
            </Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2"
                android:text="我占据2列" >
            </Button>
        </TableRow>
    </TableLayout>
    

    TableLayout继承LinearLayout,有元素单独占一行可以不写TableRow
    TableLayout的一些属性:

    • android:shrinkColumns 设置可收缩的列
    • android:stretchColumns 设置可伸展的列
    • android:collapseColumns 设置要隐藏的列
      默认所有的列为stretchColumns,如只有一个控件则独占一行
      每列宽度的计算原则:首先根据可伸展列根据其内容最长的一行占据一行的百分比,再根据可收缩的列等分剩余的部分
      例如图中,优先计算“ 111111111111111111111111”的宽度,再由剩下的列等分剩余的空间
      如果遇到空间已经不足了,则剩余的列均不显示
      控件的一些属性:
      android:layout_column表示当前控件在第几列
      android:layout_span表示合并单元格个数

    绝对布局

    难以实现多分辨率适配,不建议使用

    相对布局

    已使用约束布局替代

    约束布局

    约束布局效果图
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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">
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:gravity="center"
            android:text="验证码"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:gravity="center"
            android:text="手机号"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <EditText
            android:id="@+id/editText2"
            android:layout_width="197dp"
            android:layout_height="39dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:ems="10"
            android:hint="请输入验证码"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toStartOf="@+id/button2"
            app:layout_constraintStart_toEndOf="@+id/textView2"
            app:layout_constraintTop_toBottomOf="@+id/editText3" />
    
        <EditText
            android:id="@+id/editText3"
            android:layout_width="298dp"
            android:layout_height="40dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:ems="10"
            android:hint="请输入手机号"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/textView3"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:text="获取验证码"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText3" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="358dp"
            android:layout_height="40dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="开始"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText2" />
    </android.support.constraint.ConstraintLayout>
    

    约束控件最低支持的版本是 Android 2.3 (Gingerbread)

    相关文章

      网友评论

        本文标题:安卓六大布局

        本文链接:https://www.haomeiwen.com/subject/cojkhftx.html