Hack2 使用延迟加载以及避免代码重复
2016-03-07 22:54
393 查看
1、使用<include />标签避免代码重复
比如在我们的工程中,要经常用到一个TestView,那么可以把TextView的布局代码单独写入一个文件中,然后在其他布局中引用它就可以了。
但是这种方法有一个问题:如果要引用TextView的布局是LinearLayout怎么办?有些属性是不能用的,这样会不会发生错误呢?这个时候就需要在<include />标签里使用android:layout_* 属性。下面分别修改两个布局文件,如下:
和之前有什么不一样呢?在TextView一开始的定义中,设置它的宽度和高度都为0,然后在include的时候才指定。这样做就可以根据要调用的布局的实际情况设置宽度和高度。如果不设置,这个页脚就显示不出来。
2、通过ViewStub实现View的延迟加载
有的时候,一些布局可能不是每个用户都需要看到。比如地图(MapView),那么可以把MapView放入ViewStub标签中,实现地图的延迟加载。先看看ViewStub的介绍吧:
ViewStub 是一种不可视并且大小为0 的视图,可以延迟到运行时填充(inflate) 布局资源。当ViewStub 设置为可视或者inflate() 方法被调用后,就会填充布局资源,然后ViewStub 便会被填充的视图替代。
接下来看看怎么实现它:
逻辑是很简单的:ViewStub引用map.xml,之后ViewStub加载的就是这个布局。首先说明一下inflateId属性的意思:它就是调用ViewStub的inflate()方法或者是setVisibility()方法返回的ID。在本例中,我们直接调用setVisibility(View.VISIBLE)就可以了。如果想获取被填充的视图的引用,inflate() 方法会直接返回该引用,这样避免了再次调用findViewById() 方法。
问题:ViewStub和一般的View设置Visibility有什么不同呢?
(但是不知道为什么出现了错误,把谷歌的MapView换成一般的布局,比如TextView就正常了)
比如在我们的工程中,要经常用到一个TestView,那么可以把TextView的布局代码单独写入一个文件中,然后在其他布局中引用它就可以了。
<!-- footer_with_layout_properties.xml --> <?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_horizontal" android:text="@string/footer_text" />
<!-- activity_main.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center_horizontal" android:text="@string/hello_world"/> <include layout="@layout/footer_with_layout_properties"/> </RelativeLayout>
但是这种方法有一个问题:如果要引用TextView的布局是LinearLayout怎么办?有些属性是不能用的,这样会不会发生错误呢?这个时候就需要在<include />标签里使用android:layout_* 属性。下面分别修改两个布局文件,如下:
<!-- footer_with_layout_properties.xml --> <?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" />
<!-- activity_main.xml-修改的部分 --> <include layout="@layout/footer_with_layout_properties" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp"/>
和之前有什么不一样呢?在TextView一开始的定义中,设置它的宽度和高度都为0,然后在include的时候才指定。这样做就可以根据要调用的布局的实际情况设置宽度和高度。如果不设置,这个页脚就显示不出来。
2、通过ViewStub实现View的延迟加载
有的时候,一些布局可能不是每个用户都需要看到。比如地图(MapView),那么可以把MapView放入ViewStub标签中,实现地图的延迟加载。先看看ViewStub的介绍吧:
ViewStub 是一种不可视并且大小为0 的视图,可以延迟到运行时填充(inflate) 布局资源。当ViewStub 设置为可视或者inflate() 方法被调用后,就会填充布局资源,然后ViewStub 便会被填充的视图替代。
接下来看看怎么实现它:
<!-- TestViewStub - activity_main.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.testviewstub.MainActivity" > <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="显示地图" android:onClick="onShowMap"/> <ViewStub android:id="@+id/map_stub" android:layout_width="match_parent" android:layout_height="match_parent" android:layout="@layout/map" android:inflatedId="@+id/map_view"/> </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="match_parent" android:layout_height="match_parent" android:clickable="true" android:apiKey="my_api_key"/>
逻辑是很简单的:ViewStub引用map.xml,之后ViewStub加载的就是这个布局。首先说明一下inflateId属性的意思:它就是调用ViewStub的inflate()方法或者是setVisibility()方法返回的ID。在本例中,我们直接调用setVisibility(View.VISIBLE)就可以了。如果想获取被填充的视图的引用,inflate() 方法会直接返回该引用,这样避免了再次调用findViewById() 方法。
private View mViewStub; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewStub = findViewById(R.id.map_stub); } public void onShowMap(View v) { Log.d("sysu", "进入点击事件"); mViewStub.setVisibility(View.VISIBLE); }
问题:ViewStub和一般的View设置Visibility有什么不同呢?
(但是不知道为什么出现了错误,把谷歌的MapView换成一般的布局,比如TextView就正常了)
相关文章推荐
- Java语言实现二分法
- PHP-005
- jvm(2)-JVM内存的设置(解决eclipse下out of memory问题)
- 设计模式之一(代码用java实现)
- 10031---Java并发编程:volatile关键字解析
- C#代码用F12转到定义时,不到源代码,而到元数据
- 并发编程
- C#中static void Main(string[ ] args)中的作用及解释
- ubuntu下利用gcc编译C语言程序
- 《JAVASE 基础学习》 第一天 环境变量配置、第一个程序运行执行过程
- Python集合(set)类型的操作
- c++双链表【构造函数、运算符重载、析构函数、增删查改及逆置等】
- jvm(2)-java内存区域
- c#语句实例(排大小)
- 浅谈Java分布式计算
- Python爬取韩寒全部新浪博客
- Git上传到Github
- 类的前向声明
- Java类加载器总结
- POJ 2762 Going from u to v or from v to u? 缩点