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

 

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

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

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

如果使用如下代码:

         

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

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

     

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

    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


原文: http://www.race604.com/using-include-in-layout/

更多相关文章

  1. 使用android隐藏api实现亮度调节
  2. Android(安卓)UI Material Design交互设计(一)--BottomNavigatio
  3. Android使用的MQTT客户端
  4. Android小米(miui)获取通话记录为null解决办法
  5. Android(安卓)NDK 知识系列(五)
  6. mybatisplus的坑 insert标签insert into select无参数问题的解决
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. Android(安卓)中的 Drawable
  2. RecyclerView不显示问题
  3. 解决Notification不显示问题支持Android(
  4. android拦截短信并屏蔽系统的Notificatio
  5. Android自定义控件
  6. android电池(四):电池 电量计(MAX17040)驱动
  7. Android初步笔记
  8. Android设置透明、半透明等效果
  9. Android中文API(141) —— GridLayout
  10. android自定义属性之format介绍