135-1821-9792

Android布局优化----ViewStub、include、merge

1 StubView

专注于为中小企业提供成都网站制作、网站建设、外贸网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业江达免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

作用:StubView标签中的布局只有在需要的时候才会被渲染加载。

注意:StubView的渲染加载操作只能执行一次;不支持merge标签

使用示例:

(1)ViewStub中引用的布局




    

(2)使用ViewStub

(3)java代码中渲染加载

ViewStub stub = (ViewStub)findViewById(R.id.viewstub);
stub.inflate();
TextView stubtv = (TextView)layout.findViewById(R.id.sbtv);
stubtv.setText("hello stub!");

2 include标签

作用:将引用的布局替换到当前布局中该标签所处的位置;

注意:引用的布局非merge,设置inlude的id属性后会覆盖掉引用布局顶层layout的id;

示例1 引用布局非merge

(1)引用布局




    

(2)使用include标签


    layout="@layout/my"
    android:layout_width="50dp"
    android:layout_height="50dp">

(3)java中使用

LinearLayout sharedlayout = (LinearLayout) findViewById(R.id.include1);
TextView tv = (TextView) sharedlayout.findViewById(R.id.mytv);
tv.setText("hello include1");

示例2 引用merge布局

(1)引用布局



    

        

        
    

(2)include标签:merge布局没有id属性,所以这里的id其实没有意义

(3)java中使用

//LinearLayout layout = (LinearLayout)findViewById(R.id.include2);//通过该代码无法获取(1)                                                    中的LinearLayout,这里的layout为null
TextView mTV1 = (TextView)findViewById(R.id.mergetv1);
mTV1.setText("hello merge tv1");
TextView mtv2 = (TextView)findViewById(R.id.mergetv2);
mtv2.setText("hello merge tv2");

3 merge标签

    merge标签相当于控件的简单集合,被include到其他布局中后根据所处容器布局结合各个控件的相关属性进行布局。

注意:merge标签没有id属性

使用示例:

(1)merge布局




    

    

此时的布局为两个textview重叠并且mergetv2处于上层;

(2)merge标签使用

(3)由于该include标签处于一个竖直的linearlayout中,此时merge布局的显示效果如下:

merge text 1
merge text 2
(4)java中使用
TextView mTV1 = (TextView)findViewById(R.id.mergetv1);
mTV1.setText("hello merge tv1");
TextView mtv2 = (TextView)findViewById(R.id.mergetv2);
mtv2.setText("hello merge tv2");

当前名称:Android布局优化----ViewStub、include、merge
路径分享:http://kswsj.com/article/jidehe.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号