目录(?)[-]

  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开发相关文章

【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout的更多相关文章

  1. 【转】 Pro Android学习笔记(五二):ActionBar(5):list模式

    可以在action bar中加入spinner的下来菜单,有关spinner,可以参考Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner. list的样式和 ...

  2. 【转】 Pro Android学习笔记(五六):配置变化

    目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...

  3. 【转】 Pro Android学习笔记(五七):Preferences(1):ListPreference

    目录(?)[-] 例子1ListPreference小例子 定义一个preferences XML文件 继承PreferenceActivity 用户定制偏好的读取 第一次运行时设置缺省值 设置Cat ...

  4. 【转】 Pro Android学习笔记(五九):Preferences(3):EditText和Ringtone Preference

    目录(?)[-] EditText Preferences xml文件 设备的存贮文件 Ringtone Preferences EditText Preferences xml文件 在res/xml ...

  5. 【转】 Pro Android学习笔记(五八):Preferences(2):CheckBoxPreference

    目录(?)[-] CheckBox Preference xml文件 设备的存贮文件 复合preference 在ListPreference的例子中显示的是单选,如果是多选,可采用CheckBoxP ...

  6. 【转】 Pro Android学习笔记(五十):ActionBar(3):搜索条

    目录(?)[-] ActionBar中的搜索条 通过Menu item上定义search view 进行Searchable的配置 在activity中将search view关联searchable ...

  7. 【转】Pro Android学习笔记(五):了解Content Provider(上)

    Content Provider是抽象数据封装和数据访问机制,例如SQLite是Android设备带有的数据源,可以封装到一个content provider中.要通过content provider ...

  8. 【转】Pro Android学习笔记(五三):调试和分析(1):Debug视图和DDMS视图

    目录(?)[-] Debug视图 DDMS视图 查看应用运行状态 进入debug状态 HPROF Thread信息 Method信息 Stop 截图 UI层次架构信息 其它的 Tab中提供的功能 我们 ...

  9. 【转】 Pro Android学习笔记(五五):调试和分析(3):adb命令、模拟器控制台和StrictMode

    目录(?)[-] adb命令 模拟器Console StrictMode adb命令 我们在学习SQLite的使用,介绍过部分adb命令的使用,见Pro Android学习笔记(五):了解Conten ...

随机推荐

  1. how to add them, how to multiply them

    http://www.physics.miami.edu/~nearing/mathmethods/operators.pdf

  2. 最简单的 GitExtensions 教程(持续更新中)

    一.安装 GitExtensions 下载 GitExtensions 完全版,一直点 Next,安装全部组件. 二.将项目文件夹/文件提交到 Git 服务器(以 GitHub 为例) 新建一个文件夹 ...

  3. 洛谷P2402 奶牛隐藏

    洛谷P2402 奶牛隐藏 题目背景 这本是一个非常简单的问题,然而奶牛们由于下雨已经非常混乱,无法完成这一计算,于是这个任务就交给了你.(奶牛混乱的原因看题目描述) 题目描述 在一个农场里有n块田地. ...

  4. 【学员管理系统】0x02 学生信息管理功能

    [学员管理系统]0x02 学生信息管理功能 写在前面 项目详细需求参见:Django项目之[学员管理系统] Django框架大致处理流程 捋一下Django框架相关的内容: 浏览器输入URL到页面展示 ...

  5. JPA hibernate spring repository pgsql java 工程(二):sql文件导入数据,测试数据

    使用jpa保存查询数据都很方便,除了在代码中加入数据外,可以使用sql进行导入.目前我只会一种方法,把数据集中在一个sql文件中. 而且数据在导入中常常具有先后关系,需要用串行的方式导入. 第一步:配 ...

  6. ALV 表头 ADD_TEXT

    [转自http://lz357502668.blog.163.com/blog/static/16496743201252891452493/] CALL METHOD valid_reference ...

  7. Python:笔记(2)——函数与模块

    Python:笔记(2)——函数与模块 Python函数 关于函数 1.我们可以使用Help来查看函数的帮助信息 2.调用函数的时候,如果传入的参数数量或者类型不符合均会报错. 3.函数名其实就是一个 ...

  8. jQuery设计理念

    jQuery设计理念 引用百科的介绍: jQuery是继prototype之后又一个优秀的Javascript框架.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1. ...

  9. 常见http返回状态码

    200:表示从客户端发来的请求在服务器端被正常处理了. 302:临时重定向,该状态码表示请求的资源已经被分配了新的URI,希望用户本次能够通过新的UIRI访问. 304:未修改,服务端资源未改变,可直 ...

  10. iptables原理及使用教程

    注意 修改iptables可能导致连接断开, 对于远程连接的用户, 需要在经过充分测试后在修改, 对于懒人可以设置一个crontab, 在你修改iptables的过程中每隔30分钟清空一次iptabl ...