android 重用页面相同布局用include

在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include /> 标签来重用layout代码
    <?xml version="1.0" encoding="utf-8"?>  
    <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" style="@style/StyleLayoutMain" mce_style="@style/StyleLayoutMain"  
        xmlns:android="http://schemas.android.com/apk/res/android">  
          
        <!-- include标签内不能设置RelativeLayout属性,如android:layout_alignParentBottom,因为不起作用 -->  
        <!-- include标签内设置id属性后(android:id),其引用的布局layout内的id属性就不起作用了,怀疑是其引用的layout外层包裹了一层include标签   
            或者是覆盖了其内的属性id-->  
        <!-- 如果没有include标签,所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差。而且界面加载的时候是按照顺序加载的,前面的布局不能  
            调用其后面的布局id。而采用include后,一个include中可以引用其后的include中的布局id属性 -->  
        <include android:id="@id/titleLayout" layout="@layout/app_title" />  
      
        <include layout="@layout/app_tradelogin"/>  
              
        <include layout="@layout/app_bottom"/>  
          
    </RelativeLayout>  

转自:http://blog.csdn.net/wangljgood/article/details/6556175

猜你喜欢

转载自shirlly.iteye.com/blog/1978181
今日推荐