Android优化 —— 布局

来源:互联网 发布:三国无双7捏脸数据 编辑:程序博客网 时间:2024/05/17 14:27

ViewStub

在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局。那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为View.GONE,然后在代码中动态的更改它的可见性。这样的做法的优点是逻辑简单而且控制起来比较灵活。但是它的缺点就是,耗费资源。虽然把View的初始可见View.GONE但是在 inflate 布局的时候View仍然会被 inflate,也就是说仍然会创建对象,会被实例化,会被设置属性。也就是说,会耗费内存等资源。

推荐的做法是使用android.view.ViewStubViewStub是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件。可以为ViewStub指定一个布局,在 inflate 布局的时候,只有ViewStub会被初始化,然后当ViewStub被设置为可见的时候,或是调用了ViewStub.inflate()的时候,ViewStub所向的布局就会被 inflate 和实例化,然后ViewStub的布局属性都会传给它所指向的布局。这样,就可以使用ViewStub来方便的在运行时,要还是不要显示某个布局。

ViewStub也不是万能的,下面总结下ViewStub能做的事儿和什么时候该用ViewStub,什么时候该用可见性的控制。

首先来说说ViewStub的一些特点:

  • ViewStub只能 inflate 一次,之后ViewStub对象会被置为空。按句话说,某个被ViewStub指定的布局被 inflate 后,就不会够再通过ViewStub来控制它了。

  • ViewStub只能用来 inflate 一个布局文件,而不是某个具体的View,当然也可以把View写在某个布局文件中。

基于以上的特点,那么可以考虑使用ViewStub的情况有:

  • 在程序的运行期间,某个布局在 inflate 后,就不会有变化,除非重新启动。因为ViewStub只能 inflate 一次,之后会被置空,所以无法指望后面接着使用ViewStub来控制布局。所以当需要在运行时不止一次的显示和隐藏某个布局,那么ViewStub是做不到的。这时就只能使用View的可见性来控制了。

  • 想要控制显示与隐藏的是一个布局文件,而非某个View。因为设置给ViewStub的只能是某个布局文件的id,所以无法让它来控制某个View。所以,如果想要控制某个View(如ButtonTextView)的显示与隐藏,或者想要在运行时不断的显示与隐藏某个布局或View,只能使用View的可见性来控制。

下面来看一个实例

在这个例子中,要显示二种不同的布局,一个是用TextView显示一段文字,另一个则是用ImageView显示一个图片。这二个是在onCreate()时决定是显示哪一个,这里就是应用ViewStub的最佳地点。

先来看看布局,一个是主布局,里面只定义二个ViewStub,一个是为显示文字做的TextView布局,一个是为ImageView而做的布局:

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:gravity="center_horizontal">    <ViewStub        android:id="@+id/viewstub_demo_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="5dip"        android:layout_marginRight="5dip"        android:layout_marginTop="10dip"        android:layout="@layout/viewstub_demo_text_layout"/>    <ViewStub        android:id="@+id/viewstub_demo_image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="5dip"        android:layout_marginRight="5dip"        android:layout="@layout/viewstub_demo_image_layout"/></LinearLayout>

TextView的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <TextView        android:id="@+id/viewstub_demo_textview"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#aa664411"        android:textSize="16sp"/></LinearLayout>

ImageView的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <ImageView        android:id="@+id/viewstub_demo_imageview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/></LinearLayout>

下面来看代码,决定来显示哪一个,只需要找到相应的ViewStub然后调用其infalte()就可以获得相应想要的布局:

package com.effective;import android.app.Activity;import android.os.Bundle;import android.view.ViewStub;import android.widget.ImageView;import android.widget.TextView;public class ViewStubDemoActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.viewstub_demo_activity);        if ((((int) (Math.random() * 100)) & 0x01) == 0) {            // to show text            // all you have to do is inflate the ViewStub for textview            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_text);            stub.inflate();            TextView text = (TextView) findViewById(R.id.viewstub_demo_textview);            text.setText("The tree of liberty must be refreshed from time to time" +                      " with the blood of patroits and tyrants! Freedom is nothing but " +                      "a chance to be better!");        } else {            // to show image            // all you have to do is inflate the ViewStub for imageview            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_image);            stub.inflate();            ImageView image = (ImageView) findViewById(R.id.viewstub_demo_imageview);            image.setImageResource(R.drawable.happy_running_dog);        }    }}

运行结果:

注意事项:

某些布局属性要加在ViewStub而不是实际的布局上面,才会起作用,比如上面用的android:layout_margin*系列属性,如果加在TextView上面,则不会起作用,需要放在它的ViewStub上面才会起作用。而ViewStub的属性在inflate()后会都传给相应的布局。

参考链接:http://blog.csdn.net/hitlion2008/article/details/6737537/

0 0