zoukankan      html  css  js  c++  java
  • Android在layout xml中使用include完成静态加载

     Android在layout xml中使用include完成静态加载

    include静态加载:
    不仅可以加载布局,还可以加载控件(控件标签名要在最外层)
    include标签中有个layout属性就是专门用来加载的。

    Android的layout样式定义中,可以使用xml文件方便的实现,有时候为了模块的复用,使用include标签可以达到此目的。例如:

    <include layout="@layout/otherlayout"></div> 
    

    android开发的官方网站的说明在这里。 
    其中,有提到:

    Similarly, you can override all the layout parameters. This means that any android:layout_* attribute can be used with the <include> tag.

    意思是任何android:layout_*属性都可以应用在标签中。

    如果使用如下代码:

    <Relativelayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    
        <Textview
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/somestring"
            android:id="@+id/top" />
    
        <include layout="@layout/otherlayout"
            android:layout_below="@id/top" />
    </Relativelayout > 
    

    发现include的otherlayout,并没有在如我们预期的在id/top这个TextView下面,而是忽略了android:layout_below属性。经过Google发现,很多人遇到类似的问题。

    有解决方法是在include的外面再包一层LinearLayout,如下:

    <Linearlayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/top" >
    
        <include layout="@layout/otherlayout">
    </Linearlayout > 
    

    在Statckoverflow上找到了更好的解决方法: 解答道:必须同时重载layoutwidth和layoutheight属性,其他的layout_*属性才会起作用,否这都会被忽略掉。上面的例子应该写成这样:

    <include layout="@layout/otherlayout">
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/top" />
    

    另外,关于xml的复用,还可以使用merge标签,merge标签主要是用来优化显示的,减少View树的层级,可以参考这里:https://developer.android.com/resources/articles/layout-tricks-merge.html, 翻译版在这里:http://apps.hi.baidu.com/share/detail/20871363

  • 相关阅读:
    MySQL单实例、多实例服务脚本
    Shell之case结构条件句
    Shell之函数
    Shell之条件测试
    Shell脚本数字比较与四则运算
    Shell之分支结构
    Shell之变量的数值计算与输入
    Shell之变量子串与变量替换
    表单和框架
    HTML部分标签和代码
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/7397229.html
Copyright © 2011-2022 走看看