Android基础知识回顾——Android布局

Android布局

Android的界面是由布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦。组件按照布局的要求依次排列,就组成了用户所看见的界面

所有的布局方式都可以归类为ViewGroup的5个类别,及ViewGroup的5个直接子类。其他的一些布局都扩展自5个类,布局还分为常用布局和不常用布局,下面由我来为大家一一列举

1.LinearLayout,线性布局(常用)
这种布局比较常用,也比较简单,就是每个元素占一行。当然也可能声明为横向排放。也就是每个元素占一列。
LinearLayout按照垂直或者水平的顺序依次排列子元素,每个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个n行单列的结构,每一行只会有一个元素,而不论这个元素宽度为多少:如果是水平排列,那么将是一个单行n列的结构。如果搭建两行两列的元素,每一个元素里再包含一个LinearLayout进行水平排列
LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。假如一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框那么它们的android:layout_weight值可以是同为1.如果不等长那么分别为1和2,意思就是一个文本框张举剩余空间的三分之二,一个占据剩余空间的三分之一。
android:layout_weight遵循数值越小,重要度越高的原则

<?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>

这里写图片描述

*2. Relative Layout 相对布局(常用)*
RelativeLayout按照各子元素之间的位置关系完成布局。再次布局中的子元素里与位置相关的属性将生效。例如Android:layout_below, Android:layout_above,android:layout_centerVertical等。注意在指定位置关系时,引用的id必须在引用之前,先被定义,否则将出现异常。
RelativeLayout是Android武大布局结构中最灵活的一种布局结构,比较适合复杂界面的布局。

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
     <TextView android:id="@+id/text_01" android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_alignParentBottom="true" android:text="1"/>
     <TextView android:id="@+id/text_02" android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_above="@id/text_01" android:layout_centerHorizontal="true" android:text="2"/>
     <TextView android:id="@+id/text_03" android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_toLeftOf="@id/text_02" android:layout_above="@id/text_01" android:text="3"/>
 </RelativeLayout>

3.不常用布局
1. AbsoluteLayout,绝对位置布局

http://www.runoob.com/w3cnote/android-tutorial-absolutelayout.html

2.FrameLayout,帧布局

http://www.runoob.com/w3cnote/android-tutorial-framelayout.html

3.TableLayout,表格布局

http://www.runoob.com/w3cnote/android-tutorial-tablelayout.html

猜你喜欢

转载自blog.csdn.net/a18166064736/article/details/80567614