android 开发必知的50个诀窍 2.2 Stubview的使用
2015-03-17 16:47
267 查看
在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局。那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为View.GONE,然后在代码中动态的更改它的可见性。这样的做法的优点是逻辑简单而且控制起来比较灵活。但是它的缺点就是,耗费资源。虽然把View的初始可见View.GONE但是在Inflate布局的时候View仍然会被Inflate,也就是说仍然会创建对象,会被实例化,会被设置属性。也就是说,会耗费内存等资源。
推荐的做法是使用android.view.ViewStub,ViewStub是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件。可以为ViewStub指定一个布局,在Inflate布局的时候,只有ViewStub会被初始化,然后当ViewStub被设置为可见的时候,或是调用了ViewStub.inflate()的时候,ViewStub所向的布局就会被Inflate和实例化,然后ViewStub的布局属性都会传给它所指向的布局。这样,就可以使用ViewStub来方便的在运行时,要还是不要显示某个布局。
使用Stubview很简单,像一个普通的控件那样去使用,然后这个里面有一个layout属性,指向我们想要引用的layout
我们findviewbyid得到这个stubview后,我们可以调用inflate函数加载布局,该函数返回我们加载的布局的引用,我们可以通过这个view来查找其里面的子控件,并进行相关操作,而且我们还可以用刚才的那个stubview对象来设置VISIBLE属性来让他显示或者隐藏.
下面上代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/other_father" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是stubview指向的布局,能看到我说明我已经被加载了" />
</LinearLayout>
代码:
最后要注意的是 android:inflatedId="@+id/other_father",stubview的这个属性一定要认真对待,可以不写这个属性,但是写的话一定不能写具体的某个控件,像这里我们如果直接写那个textview的话,就会报Linearlayout 转换成textview的强制类型转换的错误,所以一定要小心了
推荐的做法是使用android.view.ViewStub,ViewStub是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件。可以为ViewStub指定一个布局,在Inflate布局的时候,只有ViewStub会被初始化,然后当ViewStub被设置为可见的时候,或是调用了ViewStub.inflate()的时候,ViewStub所向的布局就会被Inflate和实例化,然后ViewStub的布局属性都会传给它所指向的布局。这样,就可以使用ViewStub来方便的在运行时,要还是不要显示某个布局。
使用Stubview很简单,像一个普通的控件那样去使用,然后这个里面有一个layout属性,指向我们想要引用的layout
我们findviewbyid得到这个stubview后,我们可以调用inflate函数加载布局,该函数返回我们加载的布局的引用,我们可以通过这个view来查找其里面的子控件,并进行相关操作,而且我们还可以用刚才的那个stubview对象来设置VISIBLE属性来让他显示或者隐藏.
下面上代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/other_father" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是stubview指向的布局,能看到我说明我已经被加载了" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/father_view" > <Button android:id="@+id/btn_test" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示"/> <ViewStub android:id="@+id/myview_stub" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout="@layout/stub" android:inflatedId="@+id/other_father" /> </LinearLayout>
代码:
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewStub; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class TestStub extends Activity { private Button button; private ViewStub viewStub; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.weight); button = (Button) findViewById(R.id.btn_test); viewStub = (ViewStub) findViewById(R.id.myview_stub); View view = viewStub.inflate();// 刚打开程序的时候就加载布局 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String result = button.getText().toString(); if (result.equals("显示")) { button.setText("隐藏"); setShow(2); } else { button.setText("显示"); setShow(1); } } }); if(view==null) { Toast.makeText(TestStub.this, "hello null", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(TestStub.this, "hello not null", Toast.LENGTH_SHORT).show(); textView = (TextView) view.findViewById(R.id.textView1); textView.setText("test"); } } public void setShow(int witch) { switch (witch) { case 1: viewStub.setVisibility(View.VISIBLE); break; case 2: viewStub.setVisibility(View.GONE); break; default: break; } } }
最后要注意的是 android:inflatedId="@+id/other_father",stubview的这个属性一定要认真对待,可以不写这个属性,但是写的话一定不能写具体的某个控件,像这里我们如果直接写那个textview的话,就会报Linearlayout 转换成textview的强制类型转换的错误,所以一定要小心了
相关文章推荐
- android 开发必知的50个诀窍 2.1 include的使用
- android 开发必知的50个诀窍 1.
- 译作:《50 Android Hacks》中文版:《打造高质量Android应用:Android开发必知的50个诀窍》
- 《打造高质量Android应用:Android开发必知的50个诀窍》勘误支持
- 《打造高质量Android应用:Android开发必知的50个诀窍》
- 打造高质量Android应用:Android开发必知的50个诀窍
- Android开发必知的50个诀窍之一
- 《android开发必知的50个诀窍》笔记Hack-1
- 《android开发必知的50个诀窍》笔记Hack-2
- Android开发必知--使用View.setId的正确姿势
- Android开发周报:50个Android Studio使用秘诀、高质量SDK设计详解
- Android 程式开发:(二)使用Intents链接Activities —— 2.2“过滤器”冲突
- 50个Android开发技巧(20 使用MVP模式)
- 50个Android开发技巧(1,使用android:layout_weight和android:weightSum属性的使用)
- 50个Android开发技巧(2,使用延迟加载以及避免代码重复,include和viewstub)
- 50个Android开发技巧(20 使用MVP模式)
- Android开发之GridView的使用(解读谷歌官方API)
- Android之使用Android-query框架进行开发(一)
- Android开发之核心特性SearchView的开发使用(源代码分享)
- Android开发之使用WindowManager开发悬浮窗口