Android-5 TableLayout布局与适配器

合集下载

Android布局之表格布局TableLayout详解

Android布局之表格布局TableLayout详解

Android布局之表格布局TableLayout详解本⽂实例为⼤家分享了Android表格布局TableLayout的具体代码,供⼤家参考,具体内容如下1.TableLayoutTableLayout表格布局模型以⾏列的形式管理⼦控件,每⼀⾏为⼀个TableRow的对象,当然也可以使⼀个View的对象2.TableLayout的属性(全局属性)android:collapseColumns=”1,2”隐藏从0开始的索引列,列之间必须⽤逗号隔开1,2android:shrinkColumns=”1,2”收缩从0开始的索引列,当可收缩的列太宽(内容太多时)不会被挤出屏幕,列之间⽤逗号隔开1,2,你可以通过”*”代替收缩所有列,注意⼀列能同时表⽰收缩和拉伸android:stretchColumns=”1,2”拉伸从0开始的索引列,以填满剩下的多余空⽩空间,列之间必须⽤逗号隔开,1,2,你可以通过”*”代替收缩所有列,注意⼀列能同时表⽰收缩和拉伸3.TableLayout的局部属性(内部控件所⽤属性)android:layout_column=”1” 该控件显⽰在第1列android:layout_span=”2” 该控件占据两列<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="/apk/res/android"android:layout_width="match_parent" android:layout_height="match_parent"android:collapseColumns="0"android:shrinkColumns="4"><TableRowandroid:id="@+id/tablerow1"android:layout_width="wrap_content"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button1" /><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button2" /><Buttonandroid:id="@+id/button6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button3" /><Buttonandroid:id="@+id/button7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button4" /><Buttonandroid:id="@+id/button8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button555555555555555555555555" /></TableRow></TableLayout><?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"android:stretchColumns="*"><TableRowandroid:id="@+id/tablerow1"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button1" /><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button2" /><Buttonandroid:id="@+id/button6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button3" /></TableRow></TableLayout>以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

Android组件---四大布局的属性详解

Android组件---四大布局的属性详解

Android组件---四⼤布局的属性详解【声明】欢迎转载,但请保留⽂章原始出处→_→Android常见布局有下⾯⼏种:LinearLayout:线性布局。

所有的控件都是串在⼀条线上的。

RelativeLayout:相对布局。

所有的控件的位置,都是相对于⽗控件的。

FrameLayout:帧布局。

FrameLayout布局中的控件都是⼀层⼀层的。

帧布局每次添加的控件都显⽰在最上⾯,最后显⽰在界⾯上的是最后添加的⼀个控件。

TableLayout:表格布局。

表格布局可以实现的.⼀般可以使⽤线性布局实现。

AbsoluteLayout:绝对布局。

已经是废弃的状态,很少⽤了。

orientation:属性是指定线性布局的排列⽅向。

horizontal ⽔平。

线性布局默认的朝向是⽔平的。

vertical 垂直例如:android:orientation="vertical"gravity:指定当前控件⾥⾯的内容容显⽰位置。

(四⼤layout中均可使⽤)left 左边right 右边top 上边bottom 底边例如:android:gravity="center"gravity中的属性可以组合使⽤。

例如:android:gravity="bottom|right"layout_gravity:指定当前控件在⽗元素的位置。

(只在 LinearLayout 和 FrameLayout 中有效)left 左边right 右边top 上边bottom 底边centercenter_horizontalcenter_vertical例如:android:layout_gravity="center"另外,需要提⽰的是,对于 LinearLayout :当 android:orientation="vertical" 时,只有⽔平⽅向的设置才起作⽤,垂直⽅向的设置不起作⽤。

android学习——TableLayout表格布局

android学习——TableLayout表格布局

android学习——TableLayout表格布局 TableLayout表格布局 TableLayout是指将⼦元素的位置分配到⾏或列中。

Android的⼀个TableLayout有许多TableRow组成,每⼀个TableRow都会定义⼀个Row。

TableLayout容器不会显⽰Row,Column,及Cell的边框线,每个Row拥有0个或多个Cell,每个Cell拥有⼀个View对象。

在使⽤tablelayout时,应注意每⼀个cell的宽度。

我们下⾯通过XML布局和Java代码布局两种⽅式分别举例:⼀、XML⽅式布局 1、创建⼀个空⽩Activity 2、打开“res/layout/activity_main.xml”⽂件,修改成以下代码。

(1)第①部分 <?xml version="1.0" encoding="utf-8" ?>,每个XML⽂档都由XML序⾔开始,在前⾯的代码中的第⼀⾏便是XML序⾔,<?xml version="1.0">。

这⾏代码表⽰按照1.0版本的XML规则进⾏解析。

encoding = "utf-8"表⽰此xml⽂件采⽤utf-8的编码格式。

编码格式也可以是GB2312。

(2)第②部分 <LinearLayout …… 表⽰采⽤表格布局管理器。

(3)第③部分 android:layout_width="match_parent" android:layout_height="match_parent"表⽰布局管理器宽度和⾼充将填充整个屏幕宽度和⾼度。

(4)第④部分 android:stretchColumns="1"表⽰表格布局管理器中第2列内组件可以扩充到的有可⽤空间。

3、插⼊1⾏TableRow、1个⽂本TextView、1个TextEdit。

表格布局(TableLayout)_Android应用开发全程实录_[共3页]

表格布局(TableLayout)_Android应用开发全程实录_[共3页]

第3章 我的界面我作主——Activity 和布局管理器45 /> <ImageViewandroid:layout_width="25dip"android:src="@drawable/title_refreshalt" android:layout_height="25dip"android:scaleType="fitXY" android:layout_marginRight="12dip"android:layout_centerVertical="true" android:id="@+id/refresh"android:layout_toLeftOf="@+id/borderone" /> <ImageViewandroid:layout_width="wrap_content"android:src="#ffffffff"android:layout_he ight="wrap_content" android:id="@+id/borderone"android:layout_alignParentTop="true" android:layout_marginRight="12dip"android:layout_alignParentBottom="true" android:layout_alignBottom="@+id/search"android:layout_toLeftOf="@+id/search" /> <ImageViewandroid:src="@drawable/title_searchalt"android:scaleType="fitXY" android:layout_width="25dip"android:layout_alignParentRight="true" android:layout_centerVertical="true"android:id="@+id/search" android:layout_marginRight="12dip"android:layout_height="25dip" /> </RelativeLayout>代码分析:分析一下这段代码。

安卓学习之--排版TableLayout表格布局

安卓学习之--排版TableLayout表格布局

安卓学习之--排版TableLayout表格布局表格布局包含⼀系列的 TableRow对象,⽤于定义⾏(实际上你也可以使⽤其它⼦对象,将在后⾯进⾏解释)。

表格布局不为它的⾏、列和单元格显⽰表格线。

每个⾏可以包含0个以上(包括0)的单元格;每个单元格可以设置⼀个View对象.与⾏包含很多单元格⼀样,表格包含很多列。

表格的单元格可以为空.单元格可以象 HTML 那样跨列。

列的宽度由该列所有⾏中最宽的⼀个单元格决定.不过表格布局可以通过 setColumnShrinkable() ⽅法或者 setColumnStretchable() ⽅法来标记某些列可以收缩或可以拉伸. 如果标记为可以收缩,列宽可以收缩以使表格适合容器的⼤⼩。

如果标记为可以拉伸,列宽可以拉伸以占⽤多余的空间。

表格的总宽度由其⽗容器决定. 记住列可以同时具有可拉伸和可收缩标记是很重要的。

在列可以调整其宽度以占⽤可⽤空间,但不能超过限度时是很有⽤的.最后,你可以通过调⽤setColumnCollapsed() ⽅法来隐藏列。

表格布局的⼦对象不能指定 layout_width 属性.宽度永远是 MATCH_PARENT。

不过⼦对象可以定义 layout_height 属性;其默认值是WRAP_CONTENT. 如果⼦对象是 TableRow,其⾼度永远是 WRAP_CONTENT。

下⾯以⼀个实际例⼦展⽰如何设计界⾯....xml⽂件<LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"tools:context=".TwoHouseTansActivity" ><TableLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:gravity="left"><TableRowandroid:id="@+id/tableRow1"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/tvprice"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="价格"/><EditTextandroid:id="@+id/etprice"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#606060"android:textSize="16px"android:width="90dp" ></EditText><TextViewandroid:id="@+id/tvbuildarea"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="建筑⾯积" /><EditTextandroid:id="@+id/etbuildarea"android:layout_width="70dp"android:layout_height="wrap_content"android:width="85dp"/></TableRow><TableRowandroid:id="@+id/tableRow2"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/tvmf"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="买⽅ "/><TextViewandroid:id="@+id/tvmfd"android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="90dp"android:text=""/><TextViewandroid:id="@+id/tvmf"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="卖⽅" /></TableRow><TableRow><TextViewandroid:id="@+id/tvqs"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="契税 "/><EditTextandroid:id="@+id/etqs"android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:width="80dp"/><TextViewandroid:id="@+id/tvyes"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="营业帨" /><EditTextandroid:id="@+id/etyes"android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="60dp" /></TableRow><TableRow><TextViewandroid:id="@+id/tvmjyyhs"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="印花税"/><EditTextandroid:id="@+id/etmjyyhs"android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="90dp"/><TextViewandroid:id="@+id/tvmijyyhs"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="印花税" /><EditTextandroid:id="@+id/etmijyyhs"android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="60dp" /></TableRow><TableRow><TextViewandroid:id="@+id/tvmjyfws"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="交易服务税"/><EditTextandroid:id="@+id/etmjyfws"android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="80dp" /><TextViewandroid:id="@+id/tvmijyfws"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="交易服务税" /><EditTextandroid:id="@+id/etmijyfws"android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="60dp" /></TableRow><TableRow><TextViewandroid:id="@+id/tvmcqdjf"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="产权登记费"/><EditTextandroid:id="@+id/etmcqdjf"android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="80dp" /><TextViewandroid:id="@+id/tvmicqdjf"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="个⼈所得税" /><EditTextandroid:id="@+id/etmicqdjf"android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="60dp" /></TableRow><TableRow><TextViewandroid:id="@+id/tvtotal"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="合计"/><EditTextandroid:id="@+id/ettotalprice"android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="95dp" /><TextViewandroid:id="@+id/tvmtotal"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="合计" /><EditTextandroid:id="@+id/etmtotalprice"android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="70dp" /></TableRow><TableRow ><Buttonandroid:id="@+id/btncal"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="计算" /></TableRow> </TableLayout></LinearLayout>。

Android开发TableLayout属性介绍

Android开发TableLayout属性介绍

在android开发中,常见的布局方式有:LinearLayout (线性布局),RelativeLayout (相对布局),TableLayout (表格布局),AbsoluteLayout (绝对布局),FrameLayout (帧布局),今天我们主要介绍表格布局的相关属性。

TableLayout经常用到的属性有:
android:collapseColumns:以第0行为序,隐藏指定的列:
android:collapseColumns该属性为空时,效果如下图:
把android:collapseColumns=0,2--------------》意思是把第0和第2列去掉,如下图:
android:shrinkColumns:以第0行为序,自动延伸指定的列填充可用部分:
当LayoutRow里面的控件还没有布满布局时,shrinkColumns不起作用,如下图:
设置了shrinkColumns=0,1,2,布局完全没有改变,因为LayoutRow里面还剩足够的空间。

当LayoutRow布满控件时,如下图:
设置设置了shrinkColumns=2,则结果如下图,控件自动向垂直方向填充空间:
android:stretchColumns:以第0行为序,尽量把指定的列填充空白部分:
设置stretchColumns=1,则结果如下图,第1列被尽量填充(Button02与TextView02同时向右填充,直到TextView03被压挤到最后边)。

更多android开发视频入门教程,点此查看>> /course/2/。

AndroidTabLayout(选项卡布局)简单用法实例分析

AndroidTabLayout(选项卡布局)简单用法实例分析

AndroidTabLayout(选项卡布局)简单⽤法实例分析本⽂实例讲述了Android TabLayout(选项卡布局)简单⽤法。

分享给⼤家供⼤家参考,具体如下:我们在应⽤viewpager的时候,经常会使⽤TabPageIndicator来与其配合。

达到很漂亮的效果。

但是TabPageIndicator是第三⽅的,⽽且⽐较⽼了,当然了现在很多⼤神都已经开始⾃⼰写TabPageIndicator来满⾜⾃⼰的需求,在2015年的google⼤会上,google发布了新的Android Support Design库,⾥⾯包含了⼏个新的控件,其中就有⼀个TabLayout,它就可以完成TabPageIndicator的效果,⽽且还是官⽅的,最好的是它可以兼容到2.2以上版本,包括2.2。

下⾯我就举⼀个简单的例⼦来使⽤它。

这⾥使⽤的 android studio进⾏开发的,所以引⽤TabLayout很简单,只要在build.gradle中加⼊compile'com.android.support:design:22.2.0'即可。

这个使⽤是我在仿知乎的时候使⽤。

所以页⾯就和知乎很像了fragment_find.xml<LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="/apk/res-auto"android:orientation="vertical"><android.support.design.widget.TabLayoutandroid:id="@+id/tab_FindFragment_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/titleBlue"app:tabIndicatorColor="@color/white"app:tabSelectedTextColor="@color/gray"app:tabTextColor="@color/white"/><android.support.v4.view.ViewPagerandroid:id="@+id/vp_FindFragment_pager"android:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"/></LinearLayout>这⾥⾯没有什么特别的,就是添加了⼀个TabLayout和Viewpager作为上下的布局。

Android培训之五大布局讲解

Android培训之五大布局讲解

Android培训之五大布局讲解Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦。

组件按照布局的要求依次排列,就组成了用户所看见的界面。

Android的五大布局分别是LinearLayout(线性布局)、FrameLayout(单帧布局)、RelativeLayout(相对布局)、AbsoluteLayout(绝对布局)和TableLayout(表格布局)。

LinearLayout:LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。

如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。

如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。

LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。

加入一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。

如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为1和2,那么第一个文本框将占据剩余空间的三分之二,第二个文本框将占据剩余空间中的三分之一。

FrameLayout:FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。

显示效果如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二个TextView的部分位置。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第5讲TableLayout布局与适配器TableLayout 表格布局
类似HTML中的Table,以行列的方式来布局组件,但是没有边框。

它是由多个TableRow对象组成,每个TableRow是一个容器,可以有0个或多个单元格,每向TableRow添加一个组件,就增加一列。

表格布局中,列的宽度由该列中最宽的那个单元格决定
这些TableRow,不能设置layout_width和ayout_hight(设置了也没有用),宽度默认是wrap_content,根据具体的包裹内容来决定显示大小,但是每个单元格中的内容可以设置高度和宽度。

android:collapseColumns="2" 设置被隐藏的列是第?列(从0开始数)
android:shrinkColumns="2" 设置被收缩的是第?列
android:stretchColumns="1" 设置被拉伸的是第?列
举例:见教材P56注册界面
部分常用组件用法补充:
(1)ToggleButton (开关按钮)
是一个具有选中和未选择状态双状态的按钮,并且需要为不同的状态设置不同的显示文本。

默认情况下是未选中textOff,需要在前面加上一个文本标签TextView来说明显示的内容。

<ToggleButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textOff="未婚" //未选中时显示的内容
android:textOn="已婚"/> //选中时显示的内容
事件处理方式:
tb.setOnClickListener(new OnClickListener()
tb = (ToggleButton) this.findViewById(R.id.tg_marry);
tb.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(tb.isChecked()) //开关按钮选中
Toast.makeText(MainActivity.this, "已婚", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "未婚", Toast.LENGTH_SHORT).show();
}});
课后作业:
利用开关按钮来控制ImageView中显示的图片变换
(2)Spinner(列表控件)
这个控件主要就是一个列表选择框,不同于web的下拉列表,是弹出一个列表菜单供用户选择。

Spinner是View类的一个子类,位于android.widget包下,每次只显示用户选中的元素,当用户再次点击时,会弹出选择列表供用户选择。

Spinner的实现方法有两种:
一种是需要一个已有的文字列表来描述其列表的各个选项。

(这个列表可能直接以数组资源的形式来给出,如下面的方法1,也可能是从string.xml中获得若干个文字信息,再形成数组,如下面的方法2)
另外一种是:希望列表中的显示内容是自定义的样式,比如是图片+文字的样式,此时采用的自定义的适配器累来继承BaseAdapter适配器,如下面的方法3。

方法一:使用xml定义的数组资源作为数据源,直接在res/values中直接定义一个数组,再
方法二:使用一个代码中拼凑所获得的数组作为数据源,在布局文件中仅仅声明一个Spinner,并不指定其选项,而在代码中将选项以ArrayAdapter适配器的形式赋给Spinner。

ArrayAdapter是已经定义好的适配器类,它继承自BaseAdapter,可以查看源码,它将传入的数组参数中的文字,放入TextView中,将这个TextView显示到列表中的每一项,因此,采用这个方法,列表上只能显示文字,若要列表选项上的内容是复合内容(或者是自定义的样式),比如图片加文字,我们可以自己写自己的适配器,如方法三。

Spinner的事件处理。

相关文档
最新文档