首页 > 其他分享 >android布局技巧:创建高效布局

android布局技巧:创建高效布局

时间:2023-06-04 18:35:52浏览次数:45  
标签:高效 RelativeLayout 布局 使用 对齐 android LinearLayout View


Android UI工具包提供了一些布局管理器,它们使用起来相当容易,而且,大多数的时候,你只需要使用它们最基本的特征来实现UI。

执着于基本特征的使用对于创建UI来说,往往不是最高效的。一个常见的例子就是滥用LinearLayout,它将会导致View树中的View数量激增。View——更糟的是,布局管理器——添加到应用程序里都会带来一定的消耗:初始化,布局和绘制变得更加缓慢。嵌套布局的花销尤其“昂贵”,例如,如果你嵌套了一些LinearLayout,并使用了weight参数,这会导致子元素要计算两次。

让我们看一个非常简单且常见的布局例子:一个列表项,左边是一个图标,右边是标题和描述,上方是标题,下方是可选的描述。列表项可能看起来如下图:

                   

android布局技巧:创建高效布局_UI

                  

为了清楚地认识View之间(一个ImageView和两个TextView)的相对位置,下图是使用HierarchyViewer抓获的布局剪影:

                                    

android布局技巧:创建高效布局_UI_02

   实现这个布局,直接使用LinearLayout就可以了。列表项本身是一个水平的LinearLayout,里面有一个ImageView和一个垂直的LinearLayout,垂直的LinearLayout里包含两个TextView。以下是这个布局的源代码:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
        android:src="@drawable/icon" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="My Application" />
        <TextView  
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" 
            android:singleLine="true"
            android:ellipsize="marquee"
            android:text="Simple application that shows how to use RelativeLayout" />
        </LinearLayout>
</LinearLayout>

 

如果你将它作为ListView的item,它能正常工作,但却是相当浪费的。相同的布局可以使用RelativeLayout进行重写,相对于每个列表项来说,可以节省一个View,且View层级上更好,只有一层。使用RelativeLayout也很简单:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="6dip"
        android:src="@drawable/icon" />
<TextView  
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip" 
        android:layout_toRightOf="@id/icon"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:text="Simple application that shows how to use RelativeLayout" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_above="@id/secondLine"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        android:text="My Application" />
</RelativeLayout>新的实现与老的实现看起

来效果完全一致,除了一种情况。每个列表项显示两行文字:标题和可选的描述。当某一个列表项的描述不可获得时,应用程序可能希望将第二个TextView的Visibility设为GONE。LinearLayout实现版表现得很完美,但RelativeLayout实现版就有点差强人意了:

android布局技巧:创建高效布局_android_03

在RelativeLayout里,每个View都是和父元素RelativeLayout对齐或是和其它View对齐的。例如,我们声明描述部分是和RelativeLayout的底部对齐,标题位于其上并与RelativeLayout的顶端对齐。当描述GONE时,RelativeLayout不知道怎么去放置标题的底边缘。为了解决这个问题,你可以使用一个非常简单的布局参数:layout_alignWithParentIfMissing。

这个布尔参数告诉RelativeLayout:如果目标对象消失时使用自己的边缘作为锚点。例如,如果你放置一个View到另一个Visibiity属性设为GONE的View的右边,且设定alignWithParentIfMissing为true,RelativeLayout就会将其左边缘作为View的对齐锚点。在我们的这个场合,使用alignWithParentIfMissing的结果是RelativeLayout将标题部分的底部与自己的底部对齐。结果如下所示:

android布局技巧:创建高效布局_ico_04

现在,我们的布局表现得很完美了,即使描述部分的Visibility属性设为GONE。更好的是,层级更加简单,因为我们不再使用LinearLayout,而且,更加高效了。当我们使用HierarchyViewer来比较两个实现版的时候,事实就更明显了:

android布局技巧:创建高效布局_移动开发_05

另外,当你使用这么一个布局作为ListView的列表项时,这种差异就更为重要了。希望这个简单的例子能让你了解布局,了解如何优化你的UI。

标签:高效,RelativeLayout,布局,使用,对齐,android,LinearLayout,View
From: https://blog.51cto.com/u_16120380/6411576

相关文章

  • android与phonegap的相互交互
    开发环境:androidSDK+android2.3或以上的真机开发所需:cordova-2.1.0.js+sencha-touch-all-debug.js 首先把你的phonegap或sehcha项目放到assets文件夹下,然后在你的入口函数(onCreate)里添加如下:1.super.init();2.super.setBooleanProperty("loadInWebView",true);......
  • 高效的jQuery代码编写技巧大盘点
    [b]缓存变量[/b]DOM遍历是昂贵的,所以尽量将会重用的元素缓存。//糟糕h=$('#element').height();$('#element').css('height',h-20);//建议$element=$('#element');h=$element.height();$element.css('height',h-20);[b]避免全局变......
  • Android网络图片三级缓存策略
    在移动应用中,我们一般将网络图片分为三个级别,第一级别是网络层,即根据图片的url地址可以找到服务器上相应图片,获取这一层的图片会消耗流量,所以我们希望可以获取后本地就永久使用,所以就会有接下来的缓存策略;第二层缓存是在手机内存层,是将第一层的图片下载到手机内存,这种缓存读取速度......
  • Android NDK链接静态库动态库
    在NDK中使用LOCAL_LDLIBS进行链接LOCAL_LDLIBS:=/home/tsh/work/ndk-demo/dobby/libdobby.aLOCAL_LDLIBS+=-llog如果在AOSP环境中中可以使用LOCAL_SHARED_LIBRARIES:=liblogLOCAL_STATIC_LIBRARIES+=/home/tsh/work/ndk-demo/dobby/libdobby.a......
  • 类似grid网格布局的巧妙玩法
    .box{margin:20pxauto;width:800px;height:800px;}ulli{position:relative;float:leftlist-style:none;width:150px;height......
  • Kali Linux中使用Vysor对Android设备进行投屏
    在Window有很多Android投屏软件,Linux相对较少,在网上查找大部分的人都是使用的scrcpy这个工具,由于在最新版中Kali无法自带的apt仓库下载该软件┌──(junglezt㉿Ubuntu)-[~]└─$sudoaptinstallscrcpy正在读取软件包列表...完成正在分析软件包的依赖关系树...完成正在读......
  • 基于Android 网上商城系统设计与实现
    随着移动通信与Internet的飞速发展及相互融合,GPRS使无线网络高速接入到Internet成为现实,移动用户从而可以享受到Internet提供的服务。这样,移动终端不再仅是通讯网络的终端,还将成为互联网的终端。本文首先给出了系统研究背景,对当前手机操作系统发展做了简单的介绍。Android是基于Li......
  • 写给Android工程师的协程指南
    这是一份写给Android工程师的协程指南,希望在平静的2023,给大家带来一些本质或者别样的理解。引言在Android的开发世界中,关于异步任务的处理一直不是件简单事。面对复杂的业务逻辑,比如多次的异步操作,我们常常会经历回调嵌套的情况,对于开发者而言,无疑苦不堪言。当Kotlin协程出......
  • 高效稳定的通用增量 Checkpoint 详解之二:性能分析评估
    作者:雷颜菲、夏瑞、俞航翔、梅源|阿里云Flink存储引擎团队摘要:我们在“Flink1.15新功能架构解析:高效稳定的通用增量Checkpoint”【1】一文介绍了通用增量Checkpoint的原理和背后的思考以及执行性能、空间放大等方面的初步测试结果。该功能在Flink1.16中经过优化,已达到生......
  • Flutter灵活布局要掌握的两个控件Expanded和Flexible
    Expanded和Flexible介绍在Flutter中,Expanded和Flexible是两个用于控制子组件尺寸的Widget,它们都可以用于实现灵活的布局。ExpandedWidget会自动将子组件的尺寸扩展到父组件剩余的空间,而FlexibleWidget则会自动调整子组件的尺寸以适应父组件的尺寸。具体来说,ExpandedWidget......