ViewStub的使用

Main代码

 1 package layoutdemo.example.administrator.layoutdemo;
 2 
 3 import android.app.Activity;
 4 import android.support.v4.app.INotificationSideChannel;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.view.ViewStub;
 9 import android.widget.Button;
10 import android.widget.TextView;
11 /*LinearLayout:线性布局,
12 RelativeLayout:相对布局
13 TableLayout:表格布局(使用GridView代替)
14 AbsoluteLayout:绝对布局(绝对坐标排列)
15 FrameLayout:帧布局(布局叠加使用)
16 
17 Android布局原则2
18 1 将可复用的组件抽取出来通过include标签使用
19 2 ViewStub来加载不常用的组件
20 3 使用merge标签减少嵌套的使用
21 
22 <include/>的使用:
23 作用:将共用的组件抽取出来单独放到一个xml文件中,然后使用include标签导入共有布局
24 效果:提高UI的制作效率和复用效率,页能保证制作的UI布局更加规整和易于维护
25 使用:
26 如:<include layout="@layout/common_title">
27 这样就会将我们自定义的common_title这xml文件中的内容显示在我们的布局中去
28 merge作用就是合并UI布局,降低UI布局的嵌套层次,例如:将一个相对布局xml文件的根节点Relativelayout修改成merge,那么如果将这个xml文件使用
29 在主界面布局文件中,这个相对布局文件会正常显示,但却不会有Relativelayout这个布局的嵌套层次,也就是说相当于这个相对布局直接变成了主界面布局中的组件。
使用ViewStub标签来加载一些不常用的布局

作用:Viewstub标签同include标签一样可以用来引入一个外部布局,不同的是,Viewstub引入的布局默认不会扩张,
既不会占用显示也不会占用位置,从而在解析layout时节省cpu和内存
ViewStub:其作用和include一致,都是可以引入公共布局。区别是:ViewStub默认是不会加载到CPU的,只有当用户触发某些事件才会使得ViewStub加载出来。而include是直接加载到CPU的。
*/ 30 public class MainActivity extends Activity { 31 TextView textView; 32 private Button button; 33 private ViewStub stub; 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.main); 38 textView= (TextView) findViewById(R.id.text2); 39 textView.setText("我改标题了"); 40 button= (Button) findViewById(R.id.button); 41 stub= (ViewStub) findViewById(R.id.stub); 42 button.setOnClickListener(new View.OnClickListener() { 43 @Override 44 public void onClick(View v) { 45 stub.inflate();//显示隐藏布局 46 } 47 }); 48 } 49 }

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="layoutdemo.example.administrator.layoutdemo.MainActivity">
11     <include layout="@layout/content_main"/>
12     <TextView
13         android:layout_alignParentBottom="true"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="Hello World!" />
17 </RelativeLayout>

content_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent" android:layout_height="wrap_content"
 4     android:background="@color/red"
 5     android:paddingTop="10dp"
 6     android:paddingBottom="10dp"><!--padding为内边距-->
 7 <TextView
 8     android:layout_width="wrap_content"
 9     android:layout_height="wrap_content"
10     android:text="返回"
11     android:id="@+id/text1"
12     android:layout_alignParentLeft="true"
13     android:textColor="#292728"
14     android:textSize="14sp"
15     android:layout_centerVertical="true"/>
16     <TextView
17         android:textColor="#292728"
18         android:layout_centerInParent="true"
19     android:layout_width="wrap_content"
20     android:layout_height="wrap_content"
21 
22         android:text="布局优化"
23     android:id="@+id/text2"/>
24     <TextView
25     android:layout_centerVertical="true"
26         android:layout_alignParentRight="true"
27         android:layout_marginLeft="10dp"
28     android:layout_width="wrap_content"
29     android:layout_height="wrap_content"
30         android:text="功能"
31     android:id="@+id/text3"/>
32 </RelativeLayout>

content_marge.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 <ProgressBar
 6     android:layout_width="match_parent"
 7     android:layout_height="wrap_content"
 8     android:id="@+id/progress"
 9     android:layout_gravity="center_horizontal"/>
10     <TextView
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="请稍候"
14         android:layout_gravity="center"/>
15 </LinearLayout>

main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     <include layout="@layout/content_main"/>
 6 <FrameLayout
 7     android:layout_width="match_parent"
 8     android:layout_height="wrap_content">
 9     <TextView
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12 
13         android:textSize="20sp"/>
14     <include layout="@layout/content_marge"/>
15 
16 </FrameLayout>
17     <Button
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="显示隐藏内容"
21         android:id="@+id/button"/>
22     <ViewStub
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:layout="@layout/main2"
26         android:id="@+id/stub"/>
27 </LinearLayout>

main2.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3     android:orientation="vertical" android:layout_width="match_parent"
4     android:layout_height="match_parent">
5 <TextView
6     android:layout_width="wrap_content"
7     android:layout_height="wrap_content"
8     android:text="123"/>
9 </LinearLayout>

 

posted @ 2016-05-09 22:35  成功人土  阅读(497)  评论(0编辑  收藏  举报