0
点赞
收藏
分享

微信扫一扫

Android-布局优化(include , merge , ViewStub)


1.回顾

   上篇 学习 SeekBar  的知识 ,前面还 学习了 Android 的 五大布局知识 ,做了了解;

2. 重点

   (1)五大布局再讨论

   (2)include 

   (3)merge

   (4)ViewStub

3.五大布局优缺点 和使用 情况

    已使用的情况以大到下排列:

   (1)RelativeLayout 

                 灵活性高,易于使用

   (2)LinearLayout

                  线性的,垂直和水平; 布局层次一样的情况下,使用 线性布局,性能 高

   (3)FrameLayout

                布局叠加

   (4)TableLayout

                 被Gridview 代替

   (5)AbsoluteLayout

                几乎不使用

4. include

   (1)将各复用的组件 抽取出来 通过 include 标签使用

   (2)将公用的组件放在单独的xml文件中,使用  <include /> 标签引入

   (3)列子

   新建common_title 布局文件:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#abcdef"
android:paddingTop="10dp"
android:paddingBottom="10dp">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14px"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:text="返回" />
<TextView
android:id="@+id/textView4"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18px"
android:text="主页" />

<TextView
android:id="@+id/textView5"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14px"
android:text="设置" />

</RelativeLayout>


    在 activity_mian.xml 中引入:


<include layout="@layout/common_title" />


  效果图(刚刚的是 上面的 title ):

                                                   

Android-布局优化(include , merge , ViewStub)_merge


    include 引入的 布局中的 控件式可以使用的;


5.merge

   (1)合并Ui布局 ,    降低 UI布局 嵌套层次
   (2)merge 等同于 framelayout 可以取代和替代 FrameLayout

   (3)例子:就是 上面效果图中的 环形进度条

    新建 common_progress.xml


<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>

<TextView
android:id="@+id/tv_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="请稍后" />

</merge>


      在 activity_main.xml 中引入


<include layout="@layout/common_progress" />


  (4)使用 merge 的时候需要几点注意

        其父布局 ,只能有 layout_width 和 layout_height 不能有其他修饰


6.ViewStub 

  (1) 惰性加载 :它引入的布局 ,只有在触发的时候,才去加载,而不是像 include 直接加载过来,减少了 cpu和内存的压力;

  (2)新建common_text.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:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是隐藏内容" />

</LinearLayout>

  (3)在 activity_main.xml 中声明 Viewstub 标签

  注意 :这里引入的 布局文件的时候是 android:layout="" ; 和 include 不一样;


<ViewStub 
android:id="@+id/stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:layout="@layout/common_text"
/>


  (4)业务实现

          初始化控件包括 ViewStub ;

          使用 viewStub的 inflate() 方法加载 ;效果 如上图所示!!!!


btn_viewStub=(Button)findViewById(R.id.btn_viewStub);
viewStub=(ViewStub) findViewById(R.id.stub);
btn_viewStub.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
viewStub.inflate();
}
});



7.demo 免积分下载












  

举报

相关推荐

0 条评论