Android中常用的布局以及性能你了解多少?

大家都知道 在Android中常用的布局有五种:
线性布局(LinearLayout):按照垂直或者水平方向布局的一种组件。
相对布局(RelativeLayout):相对某个组件的布局方式。
帧布局(FrameLayout):组件从屏幕左上方布局组件一层一层。
表格布局(TableLayout):按照行列方式布局组件类似于表格。
绝对布局(AbsoluteLayout):按坐标来布局某个组件。


(1)首先结合实例来详细的在介绍下 这五种布局


一 线性布局
线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。
常用的属性:
android:orientation:可以设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:控制各个组件在布局中的相对大小
实例代码:
main.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    >       
    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        > 
        <EditText 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            /> 
    </LinearLayout> 
    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:gravity="left" 
        > 
   
        <Button 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="ok" 
            /> 
        <Button 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="cancel" 
            />    
     </LinearLayout> 
</LinearLayout> 


二 相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。
实例代码:
main.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    > 
    <TextView    
        android:id="@+id/tv1" 
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:layout_marginBottom="20dp" 
        android:text="测试:" 
        /> 
    <EditText 
        android:id="@+id/tx1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/tv1" 
        /> 
    <Button 
        android:id="@+id/btn1" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_below="@id/tx1" 
        android:layout_alignParentRight="true" 
        android:text="ok" 
        /> 
    <Button 
        android:id="@+id/btn2" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_below="@id/tx1" 
        android:layout_toLeftOf="@id/btn1" 
        android:layout_marginRight="20dp" 
        android:text="cancel" 
        /> 
</RelativeLayout> 


三 帧布局
帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。
实例代码:
main.xml
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView    
        android:layout_width="350dp"   
        android:layout_height="350dp"   
        android:background="#ff0000"          
        /> 
    <TextView    
        android:layout_width="250dp"   
        android:layout_height="250dp"   
        android:background="#00ff00"          
        /> 
    <TextView    
        android:layout_width="150dp"   
        android:layout_height="150dp"   
        android:background="#0000ff"          
        /> 
</FrameLayout> 


四 表格布局
表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数


实例代码:
 main.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TableRow> 
        <Button 
            android:text="测试1" 
            /> 
        <Button 
            android:text="测试2" 
            /> 
        <Button 
            android:text="测试3" 
            /> 
    </TableRow> 
    <TableRow> 
        <Button 
            android:text="测试4" 
            /> 
        <Button 
            android:layout_span="2" 
            android:text="测试5" 
            /> 
    </TableRow> 
      
</TableLayout> 




五  绝对布局
 绝对布局是Android 中最为不常用的一种布局他是,通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。所以这里我也就不再详细介绍。


(2)五种布局的性能比较
大多数用户感知到的卡顿等性能问题的最主要根源都是因为渲染性能。从设计师的角度,他们希望App能够有更多的动画,图片等时尚元素来实现流畅的用 户体验。
但是Android系统很有可能无法及时完成那些复杂的界面渲染操作。Android系统每隔16ms发出VSYNC信号,触发对UI进行渲染, 如果每次渲染都成功,
这样就能够达到流畅的画面所需要的60fps,为了能够实现60fps,这意味着程序的大多数操作都必须在16ms内完成。
至于原理相信大家都懂
简单来说,View是Android系统在屏幕上的视觉呈现,也就是说你在手机屏幕上看到的东西都是View。
而 View的绘制流程是从ViewRoot的performTraversals()方法开始,依次经过measure(),layout()和draw()三个过程才最终将一个View绘制出来。
  Android中的视图都是通过Window来呈现的,不管Activity、Dialog还是Toast它们都有一个Window,然后通过WindowManager来管理View。Window和顶级View——DecorView的通信是依赖ViewRoot完成的。
   不管简单的Button和TextView还是复杂的RelativeLayout和ListView,他们的共同基类都是View。所以说,View是一种界面层控件的抽象,他代表了一个控件。
   那ViewGroup是什么东西,它可以被翻译成控件组,即一组View。ViewGroup也是继承View,这就意味着View本身可以是单个控件,也可以是多个控件组成的控件组。
   根据这个理论,Button显然是个View,而RelativeLayout不但是一个View还可以是一个ViewGroup,而ViewGroup内部是可以有子View的,这个子View同样也可能是ViewGroup,
     RelativeLayout和LinearLayout是Android中常用的布局,两者的使用会极大的影响程序生成每一帧的性能,因此,正确的使用它们是提升程序性能的重要工作。
记得以前,较低的SDK版本新建Android项目时,默认的布局文件是采用线性布局LinearLayout,但现在自动生成的布局文件都是RelativeLayout,或许你会认为这是IDE的默认设置问题,其实不然,
这由 android-sdk\tools\templates\activities\BlankActivity\root\res\layout\activity_simple.xml.ftl 这个文件事先就定好了的,也就是说这是Google的选择,而非IDE的选择。
那SDK为什么会默认给开发者新建一个默认的RelativeLayout布局呢?
     当然是因为RelativeLayout的性能更优,性能至上嘛。但是我们再看看默认新建的这个RelativeLayout的父容器,也就是当前窗口的顶级View——DecorView,它却是个垂直方向的LinearLayout,上面是标题栏,下面是内容栏。
那么问题来了,Google为什么给开发者默认新建了个RelativeLayout,而自己却偷偷用了个LinearLayout,到底谁的性能更高,开发者该怎么选择呢?
当RelativeLayout和LinearLayout分别作为ViewGroup,表达相同布局时绘制在屏幕上时谁更快一点。
上面已经简单说了View的绘制,从ViewRoot的performTraversals()方法开始依次调用perfromMeasure、performLayout和performDraw这三个方法。
这三个方法分别完成顶级View的measure、layout和draw三大流程,其中perfromMeasure会调用measure,measure又会调用onMeasure,在onMeasure方法中则会对所有子元素进行measure,
这个时候measure流程就从父容器传递到子元素中了,这样就完成了一次measure过程,接着子元素会重复父容器的measure,如此反复就完成了整个View树的遍历。
同理,performLayout和performDraw也分别完成perfromMeasure类似的流程。通过这三大流程,分别遍历整棵View树,就实现了Measure,Layout,Draw这一过程,View就绘制出来了。
 





猜你喜欢

转载自blog.csdn.net/qq_37014804/article/details/69384413
今日推荐