当你在创建一个非常复杂的布局的时候,你会发现你自己添加了一大推的ViewGroups和Views。但是你的布局的层次越深,程序的效率就会越低。所以一个优化的布局,对于创建一个运行迅速、快速反应用户的操作的程序是非常重要的。 RelativeLayout xmlns:android=ht
当你在创建一个非常复杂的布局的时候,你会发现你自己添加了一大推的ViewGroups和Views。但是你的布局的层次越深,程序的效率就会越低。所以一个优化的布局,对于创建一个运行迅速、快速反应用户的操作的程序是非常重要的。
1 2 3 4 5 6 | <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= ".MainActivity" >
<textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_centerinparent= "true" android:gravity= "center_horizontal" android:text= "@string/hello" ></textview>
< include layout= "@layout/footer_with_layout_properties" ></ include >
</relativelayout>
|
로그인 후 복사
而footer的布局文件是如下这个样子滴:
1 | <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" ></textview>
|
로그인 후 복사
1 2 3 4 5 | <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= ".MainActivity" >
<textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_centerinparent= "true" android:gravity= "center_horizontal" android:text= "@string/hello" ></textview>
< include layout= "@layout/footer" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_alignparentbottom= "true" android:layout_marginbottom= "30dp" ></ include >
</relativelayout>
|
로그인 후 복사
下面是修改过的footer.xml:
1 | <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" ></textview>
|
로그인 후 복사
在这第二个例子当中,我们一定要注意,如果我们要使用android:layout_*之类的属性,我们必须先指定android:layout_width与android:laytout_height这两个属性。
2.2 使用ViewStub实现延迟加载视图(Views)
当我们在设计布局时,你可能会考虑到根据用户的操作来显示不同的布局。如果你以前是用让一个View变为invisible与visible来实现视图的变化的话,你就需要继续往下读了。ViewStub会成为你的更优的选择。
为了介绍一下这个ViewStub,我们先来看一下Android的官方文档:
ViewStub是一个不可见的、长度宽度为0的View,它可以在程序运行时从布局资源(layout resources)中延迟(异步?)加载。当ViewStub可见的时候,或是当inflate()函数被调用的时候,布局文件就已经被加载了。然后被加载的View或Views就会替代ViewStub。
这下你就了解了什么是ViewStub,让我们看看它可以做什么。在下面的这个例子当中,我们用ViewStub来延迟加载一个MapView。想象一下我们要展示某一个地点的具体信息,有如下的两种可能的场景:
如果当前的场景中没有GPS信息,我们就不能将一个标记放置在地图上,另外如果用户不需要看地图,我们为什么要加载它们?我们将MapView放在ViewStub中,然后让用户决定是否加载这个地图。为了实现它,我们需要下面这个布局:
1 2 3 4 5 | <?xml version= "1.0" encoding= "utf-8" ?>
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" >
<button android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/show_map" android:onclick= "onShowMap" ></button>
<viewstub android:id= "@+id/map_stub" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:layout= "@layout/map" android:inflatedid= "@+id/map_view" ></viewstub>
</relativelayout>
|
로그인 후 복사
很明显,我们需要ViewStub的Id来在Activity中获取它,它的layout属性说明了它要加载哪一个布局文件。下面是地图的布局文件:
1 2 | <?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:clickable= "true" android:apikey= "my_api_key" ></com.google.android.maps.mapview>
|
로그인 후 복사
最后一个我们需要讨论的属性是:inflatedId。inflatedId是我们在调用inflate()或setVisibility()之后使用的View的ID。在这个例子中我们使用setVisibility(View.VISIBLE),因为我们对MapView不进行任何别的操作。如果我们想获取被加载的布局的引用,我们只需要用inflate()这个方法,它会返回一个view类型来避免二次调用findViewById()。
Activity的代码很简单,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class MainActivity extends Activity {
private View mViewStub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mViewStub = findViewById(R.id.map_stub);
}
public void onShowMap(View v) {
mViewStub.setVisibility(View.VISIBLE);
}
}
|
로그인 후 복사
正如你所看到的,当我们需要展示地图的时候我们只需要变化ViewStub的Visibility就可以。
2.3 总结
是一个让你的布局井然有序的工具。如果你已经用过Fragment,你就会注意到它与使用include差不多就是一个道理。正如你对Fragments做的一样,你的界面的视图可以由好多组成。
ViewStub是一个极好的延迟加载布局的类,每当你想根据场景隐藏或是展示一个View的时候,你就可以尝试使用ViewStub。可能当只有一个View的时候你不会注意到启动速度的优化,但是当布局变得很深很复杂的时候,性能就会变得很好。
2.4 相关链接
http://code.google.com/p/android/issues/detail?id=2863
http://android-developers.blogspot.com.ar/2009/03/android-layout-tricks-3-optimize-with.html
http://developer.android.com/reference/android/view/ViewStub.html
http://blog.csdn.net/kost_/article/details/13170219
代码下载地址:
http://download.csdn.net/detail/u011418185/6461377