在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. Unable to load Virtual engine

    相关:GenyMotion自带VirtualBox 解决办法:首先确认genymotion及virtualBox正确安装,然后右键点击启动程序选属性,在兼容性里选以兼容模式运行,同时勾选以管理员身份运 ...

  2. java求最大公约数(分解质因数)

    下面是四种用java语言编程实现的求最大公约数的方法: package gcd; import java.util.ArrayList; import java.util.List; public c ...

  3. STL 源代码分析 算法 stl_algo.h -- pre_permutation

    本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie pre_permutation ------------------------------ ...

  4. zoj2588 Burning Bridges --- 寻求尖端

    #include <iostream> #include <cstring> #include <string> #include <cstdio> # ...

  5. Ubuntu升级后apache所有的失败,以解决虚拟文件夹的设置

    问题描述: 将Ubuntu离12.04升级到14.04后,出现apache配置的虚拟文件夹所有失效.所有站点域名所有定向到根文件夹.无法分别訪问! 尝试方法: 開始以为是升级后Apache的问题.已经 ...

  6. 【Linux】CentOS系统

    版本号:CentOS release 5.7 1)查看系统版本号 cat  /etc/readhat-release 2)安装软件 wget  资源链接 make make install 在线安装: ...

  7. Jquery基础教程第二版学习记录

    本文仅为个人jquery基础的学习,简单的记录以备忘. 在线手册:http://www.php100.com/manual/jquery/第一章:jquery入门基础jquery知识:jquery能做 ...

  8. 发布一个参考ssdb,使用go类似的实现redis高性能nosql:ledisdb

    起因 ledisdb是一个參考ssdb.採用go实现,底层基于leveldb,相似redis的高性能nosql数据库,提供了kv,list,hash以及zset数据结构的支持. 我们如今的应用极大的依 ...

  9. String.Split()功能

    我们在过去的教训 String.Join功能(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx).当中用到了String.SPli ...

  10. MyEclipse10.0 集成 SVN

    一:下载服务端和client工具   服务端安装工具:Setup-Subversion-1.6.5.msi client安装工具:TortoiseSVN 下载地址:http://subclipse.t ...