目录(?)[-]

  1. 布局Layout

    1. 线性布局LinearLayout
    2. 表格布局TableLayout

布局Layout

Layout是容器,用于对所包含的view进行布局。layout是view的子类,所以可以作为view嵌入到其他的layout中。Android的layout有LinearLayout、TableLayout,RelativeLayout、FrameLayout、GridLayout。

线性布局:LinearLayout

这是最常用的,有anroid:orientation来确定排列的方向。在view属性中与布局相关的常用的属性有weight和gravity。下面是一个例子垂直的布局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  ……  android:orientation="vertical" > 
     <EditText android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="one" 
        android:gravity="left" 
        android:layout_weight="0.0"/

    <EditText android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="two" 
        android:gravity="center" 
        android:layout_weight="1.0
"/> 
    <EditText android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="three" 
        android:gravity="right" 
        android:layout_weight="0.0"
/> 
</LinearLayout>

android:gravity是view中内容在该view中的位置,可以为top,buttom,left, center, right, top, bottom, center_vertical(超出范围,将上下裁剪掉), clip_horizontal,fill,fill_vertical,fill_horizion。view有另一个属性和它相似,就是android:layout_gravity,android:layout_gravity是view在容器中的位置。

   <Button android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TEST" 
        android:layout_gravity="right"/>

android:layout_weight是view所占空间的权重。0.0是比较特别的表示,表明必须占据所需的空间,不参与空间的分割。在例子中one和three都是0.0,系统为他们预留了最上和最下的位置,而two占据了1,表明剩余参与分配的空间,由于剩余只有two一个控件,全部给了two。0.0是很有用的方式,例如Pro Android学习笔记(十九):用户界面和控制(7):ListView,能够确保listview最下方留下一button的空间,无论list有多长,用户都不需要拉到最后才看到button,确保button一定出现在屏幕的下方。

表格布局:TableLayout

TableLayout是表格布局方式。下面是最简单的例子:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <TableRow > 
        <TextView android:text="One:" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/> 
        <EditText android:text="Hello" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/> 
    </TableRow>

<TableRow > 
        <TextView android:text="Two:" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/> 
        <EditText android:text="World" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/> 
    </TableRow>  
    
</TableLayout>

TableLayout的子view是TableRow,TableRow是表格的一行。但是TableLayout也可以用其它view作为它的child,每个child就是一行,即使我们设置android:layout_width=”wrap_content“也不起作用,认为都是占据一行位置。表格的列数是多少,由最多列的TableRow决定。试验如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout ......> 
    <TableRow > <!-- 有2列 --> 
        <TextView android:text="One:" ……/> 
        <EditText android:text="Hello" ……/> 
    </TableRow>

<TableRow > <!-- 有3列,表格列数为3 --> 
        <TextView android:text="Two:" …… /> 
        <EditText android:text="World" …… /> 
        <EditText android:text="History" ……/> 
    </TableRow> 
    <!-- 采用EditText作为child,占据整行,即便设置wrap_content,也是占据整行 -->
    <EditText android:text="Hello my Friend!" 
        android:layout_width="wrap_content"    
        android:layout_height="wrap_content"/> 
</TableLayout>

缺省下,表格的cell都是紧凑布局,常会导致表格的最右方让有空余位置。如果我们希望某列或者某些列能够延展,将空余位置分配给这些列,可以通过strechColumns指出需要延展的列,从0开始计算。同样的可以使用shirnkColumns设置可压缩的列,如果其他列位置不够,则压缩所设置的shirnkColumns。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout ……  
    android:stretchColumns="0,1,2" > 
    <EditText android:text="Full Space here" …… />
    <TableRow > 
        <TextView android:text="One" ……/> 
        <TextView android:text="Two" ……/> 
        <TextView android:text="Three" ……/>        
    </TableRow> 
</TableLayout>

下面,我们试验几个变化,同时温习一下控件间隔的设置。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout ……  android:stretchColumns="0,1,2"> 
    <TableRow > 
        <TextView android:text="One" …… /> 
        <TextView android:text="Two"…… /> 
        <TextView android:text="Three" …… /> 
    </TableRow>   
   <!-- 通过layout_span,cell可以占据多个位置 -->  
    <TableRow > 
        <EditText android:text="One" ……      android:layout_span="2"/>
        <EditText android:text="Two" ……/> 
    </TableRow>  
    <!-- 试验padding,padding表示在view内部,内容到view边框的距离,可以分别设置四边 -->
    <TableRow> 
        <Button android:text="One" ......     android:padding="30px"/>
        <Button android:text="Two" … … /> 
        <Button android:text="Three" … … /> 
    </TableRow>  
    <!-- 试验margin,margin是view与容器边框的距离,这是设置view外部的留空。而padding是view内部的留空,一般而言,属性中带有layout_xxx,都是与外部容器之间的关系。 -->
    <TableRow> 
        <Button android:text="One" …… /> 
        <Button android:text="Two" ……     android:layout_marginLeft="20px"/>
        <Button android:text="Three" …… /> 
    </TableRow> 
</TableLayout>

相关链接: 我的Android开发相关文章

转载于:https://www.cnblogs.com/blongfree/p/5047940.html

【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout...相关推荐

  1. Pro Android学习笔记(二九):用户界面和控制(17):include和merge

    xml控件代码重用:include 如果我们定义一个控件,需要在不同的layout中重复使用,或者在同一个layout中重复使用,可以采用include的方式.例如定义my_button.xml如下 ...

  2. 【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge

    目录(?)[-] xml控件代码重用include xml控件代码重用merge 横屏和竖屏landsacpe portrait xml控件代码重用:include 如果我们定义一个控件,需要在不同的 ...

  3. Pro Android学习笔记(三三):Menu(4):Alternative菜单

    什么是Alternative menu(替代菜单) 举个例子,Activity显示一个文本文件.如果用户想对文本文件进行编辑,Activity不提供编辑能力,但可由其他activity或者其他应用提供 ...

  4. Android学习笔记第五篇--网络连接与云服务(一)

    Android学习笔记第五篇–网络连接与云服务 第一章.无线连接设备 ​ 除了能够在云端通讯,Android的无线API也允许在同一局域网内的设备通讯,**甚至没有连接网络,而是物理具体相近,也可以相 ...

  5. 【转】 Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner

    目录(?)[-] GridView Spinner GridView GridView是网格状布局,如图所示.在了解ListView后,很容易了解GridView.下面是例子的XML文件. <? ...

  6. Pro Android学习笔记(七七):服务(2):Local Service

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ Local Service的目的是更容易实 ...

  7. Pro Android学习笔记(一五五) 传感器(5) 磁场传感器和方位(上)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 文章转载 ...

  8. Pro Android学习笔记(一五五):传感器(5): 磁场传感器和方位(上)

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei. 磁场传感器(Magne ...

  9. Pro Android学习笔记 四八 ActionBar 1 Home图标区

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Acti ...

最新文章

  1. Android runOnUiThread() 方法的使用
  2. 如何使WINCE启动时自动运行应用程序
  3. 用css来设置table的border
  4. 2015年第六届蓝桥杯C/C++ A组国赛 —— 第一题:方格填数
  5. 八张图彻底了解JDK8 GC调优秘籍-附PDF下载
  6. ea 备份码是什么_EA的原始访问是什么,值得吗?
  7. 规模化微服务——《微服务设计》读书笔记
  8. Nginx源码分析-启动初始化过程(一)
  9. 【技术文档】jeecg3.7.1-maven版本搭建环境手把手入门-eclipse
  10. 动画函数,为任意一个元素移动到指定的目标位置
  11. 魅族16T官网开启预约:骁龙855+4400mAh大电池
  12. vmware使用技巧
  13. [转载]Informix平安特征庇护数据的详细方法
  14. 反编译那些事儿(一)
  15. 股票数据下载-如何下载股票历史行情数据?
  16. 大学生体育运动网页设计模板代码 校园篮球网页作业成品 学校篮球网页制作模板 学生简单体育运动网站设计成品...
  17. 程序员如何规划自己的职业生涯
  18. 面渣逆袭:JVM经典五十问,这下面试稳了
  19. pointer在html作用,html-CSS中的“ cursor:pointer”效果为什么不起作用
  20. linux 磁盘碎片整理,Linux上没有磁盘碎片清理功能如何整理磁盘碎片

热门文章

  1. AcWing 860. 染色法判定二分图(染色法)
  2. springboot update数据_SpringBoot整合Mybatis+Druid+数据库(注解版)
  3. Tomcat和eclipse的整合
  4. php命令行是什么,什么是命令行?
  5. Ext2 中如何换行 布局?
  6. spark-shell开发wordcount程序
  7. ring0检测隐藏进程
  8. 原创:CSS3技术-雪碧图自适应缩放与精灵动画方案
  9. 【Android LibGDX游戏引擎开发教程】第06期:图形图像的绘制(下)图片整合工具的使用...
  10. 你还在烦U盘记录该如何消除吗