安卓优化一(布局优化)

布局文件优化

1. 减少重复布局

若能将代码的公共函数抽出来形成单独的公共布局文件,由各个页面布局文件分别引用
,这样会减少布局的重复使用。Android提供了相应的途径,只要在页面布局中使用include标签声明公共布局,即可实现在该页面导入公共布局内容。include标签适用于在多个布局文件中导入相同的XML布局片段。
inclue标签的使用方法很简单,只需要一行配置即可完成公共布局引用,下面代码引用了一个名为common_title.xml的公共布局文件:

<include layout="@layout/common_title"/>

公共布局文件的根节点可以是LinearLayout、RelativeLayout等布局节点,但是外部的页面布局文件往往已经有了相同的布局节点,这是子布局节点就变成冗余的了,但是布局文件必须有根布局节点,不能把控件作为根节点,为了解决根布局冗余的问题,Android提供了merge标签进行布局优化,即吧merge标签作为公共布局文件根节点。merge标签代替LinearLayout、RelativeLayout等原节点的位置。
下面是一个公共布局merge标签用法的例子:

<?xml version="1.0"?>

<merge xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">


<android.support.v7.widget.Toolbar
 app:navigationIcon="@drawable/ic_back" 
 android:background="@color/blue_light"
 android:layout_height="50dp" 
 android:layout_width="match_parent" 
 android:id="@+id/tl_head">


<RelativeLayout 
android:layout_height="wrap_content" 
android:layout_width="match_parent">

<TextView 
android:layout_height="match_parent" 
android:layout_width="wrap_content" 
android:id="@+id/tv_title" android:textSize="20sp" 
android:textColor="@color/black" 
android:gravity="center" 
android:paddingRight="50dp" 
android:layout_centerInParent="true"/>

<ImageView 
android:layout_height="match_parent" 
android:layout_width="wrap_content" 
android:id="@+id/iv_share" 
android:scaleType="fitCenter" 
android:src="@drawable/ic_share" 
android:layout_alignParentRight="true"/>

</RelativeLayout>

</android.support.v7.widget.Toolbar>

</merge>

2. 自适应调整布局

根据条件展示不同的视图常常需要设置视图的可视属性。比如调用setVisibility方法设置可视属性,若需站视则将可是图设置位View.VISIBLE,若需要隐藏可将视图属性设置位View.GONE。gone视图将会隐藏,在界面渲染时还是会被加载。要想事先不加载视图,在条件匹配是才加载,就可以使用标签ViewStub。
占位视图ViewStub类似一个简单的View,但其内布局由属性layout指定。在App加载页面时候,View并不显示布局内容,只有在代码中调用ViewStub对象的inflate方法是,layout指定布局才会展示出来。基于以上处理逻辑,ViewStub在提高布局性能上有两个特点:

  • ViewStub在加载时只占用大约一个View的内存,不占用layout整个布局的内存。

  • ViewStub一旦调用inflate方法,就立即显示包含的页面内容。如果还想再次隐藏或显示布局,就要通过setVisibility方法实现。

    举一个ViewStub实际运用的例子,手机在竖屏和横屏之间切换时,有时希望显示不同的布局,比如竖屏显示列表、横屏显示网格。如此一来,在页面布局中预留两个ViewStub节点,一个给ListView占位,另一个给GridView占位,代码如下

<?xml version="1.0"?>

<LinearLayout 
android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">

<include 
layout="@layout/common_title"/>

<ViewStub 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
android:layout="@layout/viewstub_list" 
android:id="@+id/vs_list"/>

<ViewStub 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
android:layout="@layout/viewstub_grid" 
android:id="@+id/vs_grid"/>

</LinearLayout>

3. 自定义窗口主题

想要让自己的App吸引眼球,首先的打造非同一般的主题。
自定义主题的配置可在res/values/styles.xml中定义,配置方式同一般视图的style风格配置,不同的是如何应用自定主题,一般视图可在布局文件的节点中使用style属性设置风格,对于视窗则可通过以下途径设置主题:

(1)修改AndroidManifest.xml,往application节点增加android:theme属性,表示对该App的所有页面设置指定的主题:或者往activity节点增加android:theme属性,表示对指定的活动页面单独设置主题。
(2)打开Activity代码,在setContentView方法之前调用方法setTheme(R.sytle.***)完成对页面的主题设置。
(3)如果是自定义对话框,就在Dialog的构造函数中传入指定主题的资源编号。
下面是窗口主题经常需要自定义的属性。

  • android:gravity 窗口内部的对齐。
  • android:background 窗口内部背景。
  • android:windowBackground 整个窗口背景,包括边框与内部。
  • android:windowFrame 窗口框架图像。
  • android:windowNoTitle 窗口是否不要默认的标题栏。
  • android:windowFullscreen 窗口是否全屏。
  • android:windowIsTranslucent 窗口是否半透明。
  • android:windowIsFloating 窗口是否悬浮。
  • android:windowAnimationStyle 窗口切换动画的样式。
  • android:windowEnterAnimation 进入窗口的动画。
  • android:windowExitAnimation 退出窗口的动画。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值