当你在创建一个非常复杂的布局的时候,你会发现你自己添加了一大推的viewgroups和views。但是你的布局的层次越深,程序的效率就会越低。所以一个优化的布局,对于创建一个运行迅速、快速反应用户的操作的程序是非常重要的。 RelativeLayout xmlns:android=ht
当你在创建一个非常复杂的布局的时候,你会发现你自己添加了一大推的viewgroups和views。但是你的布局的层次越深,程序的效率就会越低。所以一个优化的布局,对于创建一个运行迅速、快速反应用户的操作的程序是非常重要的。
<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"
/>
<include
layout="@layout/footer_with_layout_properties"
/>
</RelativeLayout><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" />
<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"
/>
<include
layout="@layout/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
/>
</RelativeLayout><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" />
当我们在设计布局时,你可能会考虑到根据用户的操作来显示不同的布局。如果你以前是用让一个View变为invisible与visible来实现视图的变化的话,你就需要继续往下读了。ViewStub会成为你的更优的选择。
为了介绍一下这个ViewStub,我们先来看一下Android的官方文档:
ViewStub是一个不可见的、长度宽度为0的View,它可以在程序运行时从布局资源(layout resources)中延迟(异步?)加载。当ViewStub可见的时候,或是当inflate()函数被调用的时候,布局文件就已经被加载了。然后被加载的View或Views就会替代ViewStub。
这下你就了解了什么是ViewStub,让我们看看它可以做什么。在下面的这个例子当中,我们用ViewStub来延迟加载一个MapView。想象一下我们要展示某一个地点的具体信息,有如下的两种可能的场景:
如果当前的场景中没有GPS信息,我们就不能将一个标记放置在地图上,另外如果用户不需要看地图,我们为什么要加载它们?我们将MapView放在ViewStub中,然后让用户决定是否加载这个地图。为了实现它,我们需要下面这个布局:
<?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"/> <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"/> </RelativeLayout>
<?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"/>
Activity的代码很简单,如下:
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是一个极好的延迟加载布局的类,每当你想根据场景隐藏或是展示一个View的时候,你就可以尝试使用ViewStub。可能当只有一个View的时候你不会注意到启动速度的优化,但是当布局变得很深很复杂的时候,性能就会变得很好。
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
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号