在Android界面xml文件中可以导入另一个xml文件,就能实现一个功能就是重复利用相同的xml布局

有两种方法进行处理:

include引入

定义一个布局Tab_flag.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:gravity="center_vertical"
android:maxHeight="160dp" > <ImageButton
android:id="@+id/weather_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/index_btn"
android:src="@drawable/j5" /> <View
android:id="@+id/items_divider_h"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#929292" /> <View
android:id="@+id/items_divider_v"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#929292" /> </RelativeLayout>

在另一个布局中引用这个布局

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/init_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" > <TableRow> <include
android:id="@+id/button1"
android:layout_height="160dp"
android:layout_gravity="center"
layout="@layout/tableitemview" /> <include
android:id="@+id/button2"
android:layout_height="160dp"
android:layout_gravity="center"
layout="@layout/tableitemview" /> <include
android:id="@+id/button3"
android:layout_height="160dp"
android:layout_gravity="center"
layout="@layout/tableitemview" />
</TableRow> <TableRow>
<include
android:layout_height="160dp"
android:layout_gravity="center"
layout="@layout/tableitemview" /> <include
android:layout_height="160dp"
android:layout_gravity="center"
layout="@layout/tableitemview" /> <include
android:layout_height="160dp"
android:layout_gravity="center"
layout="@layout/tableitemview" />
</TableRow> </TableLayout>

其中,

android:stretchColumns="*" 
stretchColumns是自适应宽度的属性,如果属性值为“*”则所有的空间都是自适应宽度,如果为“1”是设置TableLayout所有行的第二列为扩展列。也就是说如果没行都是三列的话,剩余的空间由第二列不齐。  <!-- include标签内不能设置RelativeLayout属性,如android:layout_alignParentBottom,因为不起作用 -->   
<!-- include标签内设置id属性后(android:id),其引用的布局layout内的id属性就不起作用了,怀疑是其引用的layout外层包裹了一层include标签或者是覆盖了其内的属性id-->   
<!-- 如果没有include标签,所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差。而且界面加载的时候是按照顺序加载的,前面的布局不能调用其后面的布局id。而采用include后,一个include中可以引用其后的include中的布局id属性 -->

merage引入

<LinearLayout
android:layout_width="100px"
android:layout_height="wrap_content">
<include layout="@layout/anotherlayout"/>
</LinearLayout> <?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<Button android:id = "@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮"/>
</merge>

这里还是没有解决一个问题,怎么样自定义相同布局中的不同内容呢?

当然是在java中写了。

TableLayout中怎么填充相同的布局的更多相关文章

  1. Android中常用的5大布局详述

    Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面. 所有的布局方式都可以归类为ViewGroup的 ...

  2. VS中C++对象的内存布局

    本文主要简述一下在Visual Studio中C++对象的内存布局,这里没有什么测试代码,只是以图文的形式来描述一下内存分布,关于测试的代码以及C++对象模型的其他内容大家可以参考一下陈皓先生的几篇博 ...

  3. 浅谈CSS3中display属性的Flex布局

    浅谈CSS3中display属性的Flex布局   最近在学习微信小程序,在设计首页布局的时候,新认识了一种布局方式display:flex 1 .container { 2 display: fle ...

  4. UI篇--Android中TableLayout中的布局

        表格布局是按照行列来组织子视图的布局.表格布局包含一系列的Tablerow对象,用于定义行(也可以使用其它子对象).表格布局不为它的行.列和单元格显示表格线.每个行可以包含个以上(包括)的单元 ...

  5. Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定

    1.listview的数据填充可以通过ArrayAdapter,SimpleAdapter,也可以是一个xml文件,但是ArrayAdapter与SimpleAdapter的区别是: 2 ArrayA ...

  6. TableLayout中stretchColumns、shrinkColumns的使用方法

    android:stretchColumns="1" android:shrinkColumns="1"这两个属性是TableLayout所特有的,也是这两个属 ...

  7. JVM高级特性-二、JVM在堆中对象的分配、布局、访问过程

    前面介绍了jvm运行时数据区域后,下面讲解下对内存中数据的其他细节,看他们是如何创建.布局及访问的 一.对象的创建 1.对象的分配 对象的创建分配方式主要有两种:指针碰撞和空闲列表 指针碰撞: 假设堆 ...

  8. Python tkinter调整元件在窗口中的位置与几何布局管理

    Tkinter中的GUI总是有一个root窗口,不管你是主动或者别动获得.主窗口就是你的程序开始运行的时候创建的,在主窗口中你通常是放置了你主要的部件.另外,Tkinter脚本可以依据需要创建很多独立 ...

  9. javaFX中解决填充(拉伸)问题

    1.margin设置实现 在项目过程中,遇到此问题,如图: 如果窗口缩小,HBox(左边的包含TitledPane那部分)看不到底部 如果窗口拉大,下面就出现空白,HBox高度没拉神 办法:对包含HB ...

随机推荐

  1. javascript小白学习指南0---1

    引言: 做为一名程序猿.都是真心的想把自己的东西分享出来,供大家一起学习探讨.一起提高技能.一起涨工资,呵   这一系列的文章都是关于Javascript 基础的 当然文章其中穿插了些我自己的理解.希 ...

  2. 于Unity3D调用安卓AlertDialog

    例如,下面的示例代码演示 package com.sample.sampletest; import android.app.AlertDialog; import android.content.D ...

  3. 初步swift该研究指出语言(基本数据类型)

    笔者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/28258805 转载请注明出处 假设认为文章对你有所帮助,请通过留言 ...

  4. cocos2d-x 网络请求

    [cocos2dx]rapidjson用法以及中文显示的解决方法 cocos2dx 读取json及解析 cocos2dx rapidjson 高速解析JSON  --- [cocos2d-x官方文档] ...

  5. IE8升级新版Flash Player ActiveX14导致的discuz图片附件无法上传 解决方法

    架不住sb adobe的频繁升级提示,手欠升级到了了flash player 14,结果IE8下全部discuz论坛中都无法看到上传图片的button了 没办法,遇到问题就解决吧 刚好在解决IE11遇 ...

  6. SICP 练习 (2.12)解决摘要 :不同的实现时间

    SICP 2.12 要求我们定义一个构造函数make-center-percent,它接收两个參数,分别代表中心点和一个误差百分比.我们须要通过这个构造函数产生一个区间.此外还须要定义一个选择函数pe ...

  7. HBase加Solr

    HBase加Solr 如何解决分布式系统数据事务一致性问题 (HBase加Solr) 摘要:对于所有的分布式系统,我想事务一致性问题是极其非常重要的问题,因为它直接影响到系统的可用性.本文以下所述所要 ...

  8. div元素上下左右居中

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  9. JS的parent对象

    top: 该变更永远指分割窗口最高层次的浏览器窗口.如果计划从分割窗口的最高层次开始执行命令,就可以用top变量. parent: 该变量指的是包含当前分割窗口的父窗口.如果在一个窗口内有分割窗口,而 ...

  10. mysql_SQL_按照日统计微博数

    主要备忘: DATE_FORMAT 函数 1:微博对比图(按日统计) SELECT DATE_FORMAT(tw.article_publish_time, '%Y-%m-%d'),count(pag ...