android连载之一 tablayout_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 4183 | 回复: 0   主题: android连载之一 tablayout        下一篇 
java_along
注册用户
等级:上尉
经验:771
发帖:36
精华:0
注册:2014-2-17
状态:离线
发送短消息息给java_along 加好友    发送短消息息给java_along 发消息
发表于: IP:您无权察看 2014-6-20 0:05:41 | [全部帖] [楼主帖] 楼主

今天开始陆续的给大家介绍android的一些知识,

希望有兴趣的同学能关注下,有问题的同学情跟帖回答,也可以加我qq840727854.

TableLayout:


Tablelayout linearlayout的一个子类,也是线性布局的一种,linearlayout就是比较简单,我这就不过多介绍了,如��有人跟帖,提出需要了解下linearlayout,那么我可以为大家介绍下。

Tablelayout 常用的xml属性:

Android:collapseColumns  设置隐藏的列

Android:shrinkColumns 设置收缩的列

Android:stretchColumns 设置拉伸的列

我一直推崇的是,从例子中去学习各种知识。

下面我们来一个简单的例子,让大家去分别感受下3个常用属性的作用,以及tablelayout的基本用法。

主要需要贴出的代码就是布局文件。也就是xml

Xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="单独一行"

/>
<TableRow >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通普通普��普通普通普通普通"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通普"

/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0,2"
>
<TableRow >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="收缩收缩"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通普通普通普通普通普通普通"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="收缩收缩"

/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,2"
>
<TableRow>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="拉伸"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="拉伸"

/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:collapseColumns="1"
>
<TableRow>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="隐藏"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通"

        />   

</TableRow>

</TableLayout>

</LinearLayout>

效果图:

北京联动北方科技有限公司

这个例子主要的布局是 外面是一个大的布局管理器LinearLayout 里面有4个布局管理器TableLayout,布局管理器大家可以理解为容器,里面可以装各种控件。

4TableLayout 代表了4个不同的例子

1)TableLayout 标签下直接加入控件标签,那么这个控件直接占一行,如图所示。

如果你不想这个控件单独占一行,那么你可以先加入<TableRow></TableRow>标签,然后在里面加入空间。

但是这样子的话就势必会造成空间不足,那么就有收缩和拉伸的问题。

看效果图的第二行,本来应该有3个按钮,但是因为第二个按钮文本太多,就把第三个按钮挤出去了。那么不想按钮被挤出去 怎么办呢?那么就要好好利用下,TableLayout提供的收缩和拉伸的属性了。

2<TableLayout 

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0,2"
>
<TableRow >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="收缩收缩"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通普通普通普通普通普通普通"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="收缩收缩"

/>
</TableRow>
</TableLayout>


第二个TableLayout 其实是3个按钮,但是第一个和第三个按钮被挤的太惨了。为什么呢?

首先,第二个按钮是普通按钮,那么它里面的文本必须是一行的(指的是TableRow里面的按钮,单独一行的按钮是可以多行的)。所以大部分空间已被占用。而在

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0,2"
>


中已经声明收缩的2列为第一列和第三列,那么剩余的一点点空间就由它们分。所以被挤的可怜。

3

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,2"
>
<TableRow>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="拉伸"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="拉伸"

/>
</TableRow>
</TableLayout>


首先要看中间的按钮,正好是文本的宽度,因为这是正常的按钮,在

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,2"
>


中指出了第1,3列是拉伸列,所以剩下的空间由它们分。

4<TableLayout 

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:collapseColumns="1"
>
<TableRow>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="隐藏"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"


        android:text="普通"

/>
</TableRow>
</TableLayout>
</LinearLayout>


这个最简单,直接

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:collapseColumns="1"
>


那么被指出的第二列就被隐藏了。

结论:隐藏就是直接把这个列弄没了,就等于少了这个控件。

如何收缩,如何拉伸呢?

以按钮为例。

其实可以分为3种按钮,正常按钮,收缩按钮,拉伸按钮。

正常的按钮的宽度就是里面文本的长度。

收缩按钮的宽度要小于或等于里面文本的长度。

拉伸按钮的宽度要大于或等于里面文本的长度。

收缩按钮是为了不让TableRow的总宽度超过屏幕的宽度,如果把总宽度(都按正常按钮来算)本来就不超过屏幕宽度那么收缩按钮完全可以当做正常按钮来算如果超过,那么收缩按钮的宽度会减小,使TableRow的总宽度等于父控件宽度。

拉伸按钮是为了让TableRow的总宽度等于父控件的宽度,如果宽度已经大于或者等于父控件,那么就把拉伸按钮当做正常按钮。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论