在开发应用程序的时候,会遇到这样的情况,在运行时动态的根据条件来决定显示哪个View或哪个布局,可以把可能用到的View都写在上面,先把他们的可见性设置为View.GONE,然后在代码中动态的更改它的可见性,这个做法优点是逻辑简单且控制起来比较灵活,缺点是耗费资源。

另外,我们可以使用ViewStub,ViewStub是一个轻量级的View,占用资源非常小的控件。可以为ViewStub指定一个布局,在Inflate布局的时候,只有ViewStub会被初始化,然后当ViewStub被设置为可见的时候或是调用了ViewStub.inflate()的时候,ViewStub所向的布局就会被Inflate实例化,然后ViewStub的布局属性就会传给他所指向的布局。

特点:

1.ViewStub只能Inflate一次,即某个ViewStub指定的布局被Inflate后,就不能再通过ViewStub来控制他了

2.ViewStub只能用来Inflate一个布局文件,而不是某个具体的View,当然可以吧View卸载某个布局文件中

activity_main.xml

<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">
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/show AAA""/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/show BBB"/> <ViewStub
android:id="@+id/aaa"
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/aaa"/> <ViewStub
android:id="@+id/bbb"
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/bbb"/>
</LinearLayout>

aaa.xml

<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAA"/> </LinearLayout>

bbb.xml

<?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" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BBB"/> </LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
Button btn1;
Button btn2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
ViewStub stub=(ViewStub) findViewById(R.id.aaa);
stub.inflate();
}
});
btn2=(Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
ViewStub stub=(ViewStub) findViewById(R.id.bbb);
stub.inflate();
}
}); } }

注意事项:

某些不技术型要加载ViewStub上才会起作用,加到实际布局上不会起作用,例如android:layout_margin*系列属性,如果加在TextView上不会起作用,需要加载ViewStub上,而ViewStub的属性在inflate()会都传给相应的布局

最新文章

  1. 在Android中Intent的概念及应用(二)——Intent过滤器相关选项
  2. Nginx + Tomcat Windows下的负载均衡配置
  3. 使用VisualVM分析性能
  4. CUDA2.3-原理之任意长度的矢量求和与用事件来测量性能
  5. Web程序员开发App系列 - 认识HBuilder
  6. sqlserver 获取时间年月日时分秒
  7. IT国家重点实验室
  8. 2.9 Model Selection and the Bias–Variance Tradeoff
  9. Facebook发布C++ HTTP框架Proxygen
  10. 11 OptionsMenu 菜单
  11. LeetCode(25)-symmetric tree
  12. 不可思议的纯 CSS 实现鼠标跟随效果
  13. [Swift]LeetCode414. 第三大的数 | Third Maximum Number
  14. android屏蔽系统锁屏的办法
  15. jenkins 可以设置最多执行并发执行多少个
  16. 《Small Memory Software:Patterns For System With Limited Memory》读书笔记
  17. CH1808 Milking Grid
  18. BZOJ5074 小B的数字
  19. export、exports、modules.exports 和 require 、import 的一些组合套路和坑
  20. 百度地图 Infowidow 内容(content 下标签) 点击事件

热门文章

  1. 算法学习-&gt;求解三角形最小路径
  2. 记一次,因表变量导致SQL执行效率变慢
  3. k8s入坑之路(8)kube-proxy详解
  4. MySQL——DML数据增、删、改
  5. v-bind使用
  6. Solon &amp; Solon Cloud 1.5.62 发布,轻量级 Java 基础开发框架
  7. 手把手教你基于Netty实现一个基础的RPC框架(通俗易懂)
  8. JVM-学习笔记持续更新
  9. [hdu7044]Fall with Fake Problem
  10. HouseRobber II