使用延迟加载以及避免代码重复
2015-10-25 19:28
253 查看
在XML文件中使用include标签来避免代码的重复以及使用ViewStub类来实现视图的延迟加载。
footer_with_layout_properties布局文件如下:
在上述示例代码中,我们使用了include 标签,并且只需要指定该标签的layout属性值。读者可能回想:“这种方式之所以可行是因为Activity在main布局文件中使用的是RelativeLayout。如果其中一个Activity的布局文件使用的是LinearLayout呢?虽然android:layout_alignParentBottom=”true”适用于RelativeLayout,但并不适用于LinearLayout。”这个想法是正确的。接下来分析使用include 标签的第二种方法,这这种方法里,我们直接在include 标签里使用android:layout_*属性。
以下是修改后的main.xml文件,其中使用了include 标签的android:layout_*属性:
footer布局文件如下:
在第二种方法中,我们通过include 标签指定footer布局文件的位置。Android的缺陷(Issue)跟踪系统中报告过一个缺陷,缺陷的标题是:”include 标签失效了,如果想通过include 标签的属性覆盖被包含的布局所指定的属性是行不通的。“这个issue描述的问题在一定程度上是正确的,问题出在如果想在include 标签中覆盖被包含的布局所指定的任何android:layout_*属性,必须在include 标签中同时指定android:layout_width和android:layout_height这两个属性。
我们把所有android:layout_*属性都移到include 标签中了,而footer.xml文件中的android:layout_width和android:layout_height属性都指定为0dp。这么做的目的是由footer.xml文件的使用者在include 标签中指定android:layout_width和android:layout_height属性。如果使用者不指定这两个属性,它的默认值都是0,我们便看不到footer布局。
ViewStub的开发文档介绍:
”ViewStub是一个不可视并且大小为0的视图,可以延迟到运行时填充(inflate)布局资源。当ViewStub设置为可视或者inflate()方法被调用后,就会填充布局资源,然后ViewStub便会被填充的视图代替。“
Activity使用的布局文件如下:
map.xml文件:
Activity源码:
无论在什么情况下,只要开发者根据上下文选择隐藏或者显示一个视图,都可以用ViewStub实现。或许并不会因为一个视图的延迟加载而感觉到性能的明显提升,但是如果视图树的层次很深,便会感觉到性能上的差距了。
使用include标签来避免代码重复
某一Activity的XML布局文件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="match_parent"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onShowMap" android:text="@string/show_map"/> <include layout="@layout/footer_with_layout_properties"/> </RelativeLayout>
footer_with_layout_properties布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp" android:gravity="center" android:text="@string/footer_text"/>
在上述示例代码中,我们使用了include 标签,并且只需要指定该标签的layout属性值。读者可能回想:“这种方式之所以可行是因为Activity在main布局文件中使用的是RelativeLayout。如果其中一个Activity的布局文件使用的是LinearLayout呢?虽然android:layout_alignParentBottom=”true”适用于RelativeLayout,但并不适用于LinearLayout。”这个想法是正确的。接下来分析使用include 标签的第二种方法,这这种方法里,我们直接在include 标签里使用android:layout_*属性。
以下是修改后的main.xml文件,其中使用了include 标签的android:layout_*属性:
<?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="match_parent"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onShowMap" android:text="@string/show_map"/> <include layout="@layout/footer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp"/> </RelativeLayout>
footer布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dp" android:layout_height="0dp" android:gravity="center" android:text="@string/footer_text"/>
在第二种方法中,我们通过include 标签指定footer布局文件的位置。Android的缺陷(Issue)跟踪系统中报告过一个缺陷,缺陷的标题是:”include 标签失效了,如果想通过include 标签的属性覆盖被包含的布局所指定的属性是行不通的。“这个issue描述的问题在一定程度上是正确的,问题出在如果想在include 标签中覆盖被包含的布局所指定的任何android:layout_*属性,必须在include 标签中同时指定android:layout_width和android:layout_height这两个属性。
我们把所有android:layout_*属性都移到include 标签中了,而footer.xml文件中的android:layout_width和android:layout_height属性都指定为0dp。这么做的目的是由footer.xml文件的使用者在include 标签中指定android:layout_width和android:layout_height属性。如果使用者不指定这两个属性,它的默认值都是0,我们便看不到footer布局。
使用ViewStub类实现View的延迟加载
设计布局的时候,读者可能想过根据上下文或者用户交互情况显示一个视图。如果想要一个视图只在需要的时候显示,你可以尝试使用ViewStub这个类。ViewStub的开发文档介绍:
”ViewStub是一个不可视并且大小为0的视图,可以延迟到运行时填充(inflate)布局资源。当ViewStub设置为可视或者inflate()方法被调用后,就会填充布局资源,然后ViewStub便会被填充的视图代替。“
Activity使用的布局文件如下:
<?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="match_parent"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onShowMap" android:text="@string/show_map"/> <!--inflatedId是调用ViewStub的inflate()方法或者setVisibility()方法时返回的ID,这个ID便是被填充的View 的ID。在本例中,我们不需要操作被填充的View,只需要调用setVisibility(View.VISIBLE)即可。如果想获取 被填充的视图的引用,inflate()方法会直接返回该引用,这样避免了再次调用findViewById()方法。--> <ViewStub android:id="@+id/map_stub" android:layout_width="fill_parent" android:layout_height="fill_parent" android:inflatedId="@+id/map_view" android:layout="@layout/map"/> <include layout="@layout/footer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp"/> </RelativeLayout>
map.xml文件:
<?xml version="1.0" encoding="utf-8"?> <com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="my_api_key" android:clickable="true"/>
Activity源码:
package com.example.huangfei.demo; import android.os.Bundle; import android.view.View; import android.view.ViewStub; import com.google.android.maps.MapActivity; public class MainActivity extends MapActivity { private ViewStub mViewStub; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewStub = (ViewStub) findViewById(R.id.map_stub); } @Override protected boolean isRouteDisplayed() { return false; } public void onShowMap(View view){ mViewStub.setVisibility(View.VISIBLE); //mViewStub.inflate(); } }
无论在什么情况下,只要开发者根据上下文选择隐藏或者显示一个视图,都可以用ViewStub实现。或许并不会因为一个视图的延迟加载而感觉到性能的明显提升,但是如果视图树的层次很深,便会感觉到性能上的差距了。
相关文章推荐
- JAVA多线程实现的三种方式
- android studio 导入eclipse工程的问题
- 写让别人能读懂的代码
- 共享代码——航班预订管理系统UFT脚本代码
- 有意思的Python:开发和部署一览
- Struts2.xml文件中的常量、全局错误的配置及相关动态方法的调用
- C++动态库调用配置
- Python:使用threading模块实现多线程(转)
- 递归实现 1+2+3+...100 php和java
- 【知识点整理(java)】Java Thread简单使用注意点汇总(三)
- git ,github的简单使用步骤
- C++primer笔记(21)
- java保留小数后n位
- 深入C++中构造函数、拷贝构造函数、赋值操作符、析构函数的调用过程总结
- C++标准库头文件
- 深入理解C++虚函数表
- Python多线程学习
- 回调函数在C/C++中的使用
- 回调函数在C/C++中的使用
- 为什么设计模式java实现的书比较多,C++却很少.