有一句古话:不论黑猫白猫,能抓到耗子就是好猫。这个也许在某些方面是有道理的,但对于我们追求精益求精的思想是背道而驰的,往往就是因为满足于一个结果,而放弃探求更加优化的处理方法。
当关注应用程序或者游戏所达到的结果时,往往非常容易忽视一些优化的问题,例如内存优化,线程优化,Media优化和UI优化等等。不同的模块都存在更为巧妙的方式来对待一般性问题,所以每当我们实现一个行为后,稍微多花一些时间来考虑目前所作的工作是否存在更为高效的解决办法。
这一次我们对常用的UI Layout优化说起,这个例子转载于 android developing blog
在Android中最常用LinearLayout表示UI的框架,而且也是最直观和方便的方法。例如创建一个UI用于展现Item的基本内容。如图所示:
线框图:
直接可以通过LinearLayout快速的实现这个UI的排列:
<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>
|
<!-- Easy AdSenser V2.37 --><!-- Post[count: 2] -->
尽管可以通过Linearlayout实现我们所预想的结果,但是在这里存在一个优化的问题,尤其是针对为大量Items。比较RelativeLayout和LinearLayout,在资源利用上前者会占用更少的资源而达到相同的效果,以下是用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>
|
针对RelativeLayout有一点需要注意,因为它内部是通过多个View之间的关系而确定的框架,那么当其中某一个View因为某些需要调用GONE来完全隐藏掉后,会影响与其相关联的Views。Android为我们提供了一个属性 alignWithParentIfMissing 用于解决类似问题,当某一个View无法找到与其相关联的Views后将依据alignWithParentIfMissing的设定判断是否与父级View对齐。
下边是两种不同layout在Hierarchy Viewer中的层级关系图:
分享到:
相关推荐
Android UI 优化.ppt
资源名称:精彩绝伦的Android UI设计:响应式用户界面与设计模式资源截图: 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
Android UI优化之merge标签的使用,主要介绍merge方法使用的注意事项及方法实现。
Android UI 设计技巧 Android UI 设计技巧是 Android 开发中的一个重要方面,它直接影响到用户体验。本文将分享 Android 中 UI 设计的一些技巧,包括按钮的 UI 设计和 ListView 以及 GridView 的 UI 设计。 按钮的...
西 安 邮 电 大 学 (计算机学院) 课内实验报告 实验名称: 界面设计:基本组件 专 业: 网络工程 班 级: 姓 名: 学 号: 指导教师: 日 期: 2017年4月20日... 编写布局代码,如下 <LinearLayout xmlns:android = ...
编写布局代码,如下 <LinearLayout xmlns:android = android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical"> <!-- 主布局中添加文本框和输入框 --> ...
android ui设计 android源码 android开源项目 android项目代码 androidui设计,UI小练习
Android开发是为Google的Android操作系统创建应用程序的过程。Android是目前全球智能手机和平板电脑市场上最流行的操作系统之一。以下是一些Android开发的核心概念以及我的一些心得体会: ### Android开发的核心...
本文带您全面了解Android开发规范,其中包括Android编码规范,Android性能优化和Android UI优化,读完绝对不会后悔的好文章。 一、Android编码规范 1.java代码中不出现中文,最多注释中可以出现中文 2.局部变量...
android ui设计:响应式用户界面与设计模式
谷歌官方发布视频
《Android UI设计》面向创建移动应用的产品经理、设计师和开发者,系统讲解了从事Android UI设计必须要掌握的Android平台的主要技术和特性,全面总结了Android UI的设计原理、设计理念和设计模式,并通过一个综合的...
115转存助手ui优化版3.4.1
android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/icon_click_wd" android:gravity="center" android:text="我的" android:textSize="10dp" /> --设置值,显示气泡 rb_...
在我们的电子商务应用程序UI上有两个页面,一个是产品页面,一个页面是水平类别列表,然后是我们产品列表。 然后在详细信息页面上,通过立即购买按钮显示产品的价格和简短描述。 我们的电子商务应用程序最好的部分是...
Android UI开发专题 Android UI开发专题(一) 之界面设计 近期很多网友对Android用户界面的设计表示很感兴趣,对于Android UI开发自绘控件和游戏制作而言掌握好绘图基础是必不可少的。本次专题分10节来讲述,有关...
用于React Native的UI工具集和组件库 查看我们的。 我们的 下载我们的Expo演示应用 (您将需要Expo App) 正在安装 请参阅安装说明。 新的主要版本5.0 查看 对于React Native> = 0.60.0 请使用react-native-ui-...
Android UI 界面设计 移动APP
是一个工具包用来帮助设计和开发 Android 用户界面,包含三个单独的工具:Android Asset Studio用户界面原型模具,Android 设计预览,时常需要重复确认程序版面设计状况的 Android App 开发者,应该会爱上这个轻量级...
自学Android之UI组件:(二)Fragment的进阶使用