Android布局技巧之包括合并与ViewStub标签的巧用

  

  

在开发中UI布局是我们都会遇到的问题,随着UI越来越多,布局的重复性、复杂度也会随之增长。

  

相信大家经常听到包括合并、ViewStub这样的标签,官方也提到这三种布局可用于布局的优化。今天就介绍下这三种布局的使用,记录下来,便于后续应用中的使用。

  

  

应用开发过程中,会遇到不同页面里有相同的布局,这时我们可以将这些通用的布局提取出来到一个单独的布局文件里,再使用& lt; include>标签引入到相应的页面布局文件里,主要通过包括的布局属性引用只
  

  

举个栗子,
  

  

包括的布局:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: id=癅 + id/容器”   android: layout_width=" match_parent "   android: layout_height=皐rap_content”比;      & lt; TextView   android: id=癅 + id/tv1”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android:文本="这里是来自包括布局”/比;      & lt;/RelativeLayout>      

活动的布局:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width=" match_parent "   android: layout_height=癿atch_parent”比;      & lt; TextView   android: id=癅 + id/tv2”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android:文本="以下的内容来自包括标签”/比;      & lt;包括   android: id=癅 + id/容器”   布局=" @layout/include_layout”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_below=癅 + id/电视”   android: layout_marginTop=" 10 dp/比;   & lt;/RelativeLayout>      


  

  

1,如果给包括标签和包括所加载的布局都添加id的话,那么id要保持一致,如例子中都是容器,否则是在代码中获取不到RelativeLayout容器的。当然我们可以避免这样的问题,只需要给其中一项添加id属性就可以。

  

2,包括布局里元素的id要和,包括所在页面布局里的其他元素id不同,如例子中的两个textview,如果把id设置相同了,程序运行起来并不会报的错,但是textview的赋值只会赋值给其中的一个。

  

3,如果需要给包括标签设置位置属性的话,如例子中的layout_below, layout_marginTop,这时候必须同时设置包括标签的宽高属性layout_width, layout_height,否则编译器是会报错的。一般情况不需要设置包括的其他属性,直接加载布局文件<代码> & lt;包括布局=癅layout/....”/祝辞

  

4,布局中可以包含两个相同的包括标签,如下代码所示两个包括都加载<代码>布局=癅layout/include_layout”

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width=" match_parent "   android: layout_height=癿atch_parent”比;      & lt; TextView   android: id=癅 + id/电视”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android:文本="以下的内容来自包括标签”/比;      & lt;包括   android: id=癅 + id/容器”   布局=" @layout/include_layout”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_below=癅 + id/电视”   android: layout_marginTop=" 10 dp/比;      & lt;包括   android: id=癅 + id/container2”   布局=" @layout/include_layout”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_marginTop=" 80 dp/比;   & lt;/RelativeLayout>      

可以设置不同包括的id属性,引用的时候如下可以正常显示:

        视图视图=findViewById (R.id.container2);   TextView TextView=view.findViewById (R.id.tv);   textView。setText(“这里是来自第二个包括布局”);      

  

合并标签可用于减少视图层级来优化布的局,可以配合包括使用,如果包括标签的父布局和包括布局的根容器是相同类型的,那么根容器的可以使用合并代替又是;

Android布局技巧之包括合并与ViewStub标签的巧用